ofcourse there are many posts available on to generate rows dynamically on button click..
I would like to share the code of scenario where in the clickevent (commandname) of buttonfield in gridview would dynamically generate the rows ....
The buttonfield is binded to gridview whose commandname is accessed in the RowBound event of gridview as shown below...
protected void gvStores_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "myCommandAdd")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = gvStores.Rows[index];
TableCell store = selectedRow.Cells[6];
al.Add(Convert.ToInt16(store.Text));
CreateTextBoxControls1(al);
}
method CreateTextBoxControls1() takes input of various click events of gridview which are stored in a static arraylist.
private void CreateTextBoxControls1(ArrayList al)
{
int rowCount = al.Count;
Table tbl = new Table();
if (tbl.GetType().ToString().Equals("System.Web.UI.WebControls.Table") && Placeholder1.FindControl("tbl") == null)
{
tbl.ID = "tbl";
}
tbl.EnableViewState = true;
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
Label _lblBookStore = new Label();
Button _btnDelete = new Button();
_lblBookStore.ID = "lblBookStore" + (rowIndex + 1).ToString();
_lblBookStore.Text =""+al[rowIndex];
tc.Controls.Add(_lblBookStore);
_btnDelete.ID = "btnDelete" + (rowIndex + 1).ToString();
_btnDelete.Text = "MyBookstore";
_btnDelete.Click += new EventHandler(_btnDelete_Click);
tc.Controls.Add(_btnDelete);
tr.Cells.Add(tc);
tblHelp.Rows.Add(tr);
}
Placeholder1.Controls.Add(tbl);
}
thus the place holder gets various rows added to the page dynamically and can associate an AJAX effect by binding the place holder and gridview inside an update panel.
No comments:
Post a Comment