In aspx page
- <form id="form1" runat="server">
- <div>
- <asp:Table ID="tbl" runat="server">
- </asp:Table>
- <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
- onselectedindexchanged="DropDownList1_SelectedIndexChanged">
- <asp:ListItem>Select...</asp:ListItem>
- <asp:ListItem>Generate</asp:ListItem>
- </asp:DropDownList>
- <asp:Button ID="btnSet" runat="server" Text="Button" onclick="btnSet_Click" /> </div>
- </form>
<form id="form1" runat="server">
<div>
<asp:Table ID="tbl" runat="server">
</asp:Table>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Select...</asp:ListItem>
<asp:ListItem>Generate</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnSet" runat="server" Text="Button" onclick="btnSet_Click" /> </div>
</form>
To create dynamic control:
- protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (DropDownList1.SelectedIndex == 1)
- {
- CreateDynamicTable();
- }
- }
- private void CreateDynamicTable()
- {
-
-
-
- int tblRows = 5;
- int tblCols = 5;
-
- for (int i = 0; i < tblRows; i++)
- {
- TableRow tr = new TableRow();
- for (int j = 0; j < tblCols; j++)
- {
- TableCell tc = new TableCell();
- TextBox txtBox = new TextBox();
- txtBox.ID = "txt-" + i.ToString() + "-" + j.ToString();
- txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
-
- tc.Controls.Add(txtBox);
-
- tr.Cells.Add(tc);
-
- }
-
- tbl.Rows.Add(tr);
- tbl.EnableViewState = true;
- ViewState["tbl"] = true;
- }
- }
<br /><br />protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)<br /> {<br /> if (DropDownList1.SelectedIndex == 1)<br /> {<br /> CreateDynamicTable();<br /> }<br /> }<br />private void CreateDynamicTable()<br /> {<br /> <br /> // Fetch the number of Rows and Columns for the table <br /> // using the properties<br /> int tblRows = 5;<br /> int tblCols = 5;<br /> // Now iterate through the table and add your controls <br /> for (int i = 0; i < tblRows; i++)<br /> {<br /> TableRow tr = new TableRow();<br /> for (int j = 0; j < tblCols; j++)<br /> {<br /> TableCell tc = new TableCell();<br /> TextBox txtBox = new TextBox();<br /> txtBox.ID = "txt-" + i.ToString() + "-" + j.ToString(); <br /> txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;<br /> // Add the control to the TableCell<br /> tc.Controls.Add(txtBox);<br /> // Add the TableCell to the TableRow<br /> tr.Cells.Add(tc);<br /> <br /> }<br /> // Add the TableRow to the Table<br /> tbl.Rows.Add(tr);<br /> tbl.EnableViewState = true;<br /> ViewState["tbl"] = true;<br /> } <br /> }<br />
On Button click
- protected void btnSet_Click(object sender, EventArgs e)
- {
- foreach (TableRow tr in tbl.Controls )
- {
- foreach (TableCell tc in tr.Controls)
- {
-
- if (tc.Controls[0] is TextBox)
- {
- Response.Write(((TextBox)tc.Controls[0]).Text);
- }
- }
- Response.Write("
- ");
- }
-
- }
<br /><br />protected void btnSet_Click(object sender, EventArgs e)<br /> {<br /> foreach (TableRow tr in tbl.Controls )<br /> {<br /> foreach (TableCell tc in tr.Controls)<br /> {<br /><br /> if (tc.Controls[0] is TextBox)<br /> { <br /> Response.Write(((TextBox)tc.Controls[0]).Text); <br /> }<br /> }<br /> Response.Write("<br/>"); <br /> }<br /> <br /> }<br />
This time No output because dynamic controls are lost in postback then what to do.
So we need to save dynamic controls value and generate dynamic controls again.we need to maintain viewstate.
- protected override object SaveViewState()
- {
- object[] newViewState = new object[2];
-
- List txtValues = new List();
-
- foreach (TableRow row in tbl.Controls)
- {
- foreach (TableCell cell in row.Controls)
- {
- if (cell.Controls[0] is TextBox)
- {
- txtValues.Add(((TextBox)cell.Controls[0]).Text);
- }
- }
- }
-
- newViewState[0] = txtValues.ToArray();
- newViewState[1] = base.SaveViewState();
- return newViewState;
- }
- protected override void LoadViewState(object savedState)
- {
-
- if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[] )
- {
- object[] newViewState = (object[])savedState;
- string[] txtValues = (string[])(newViewState[0]);
- if (txtValues.Length > 0)
- {
-
- CreateDynamicTable();
- int i = 0;
- foreach (TableRow row in tbl.Controls)
- {
- foreach (TableCell cell in row.Controls)
- {
- if (cell.Controls[0] is TextBox && i < txtValues.Length)
- {
- ((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();
-
- }
- }
- }
- }
-
- base.LoadViewState(newViewState[1]);
- }
- else
- {
- base.LoadViewState(savedState);
- }
- }
<br /><br />protected override object SaveViewState()<br /> {<br /> object[] newViewState = new object[2];<br /><br /> List<string> txtValues = new List<string>();<br /> <br /> foreach (TableRow row in tbl.Controls)<br /> {<br /> foreach (TableCell cell in row.Controls)<br /> {<br /> if (cell.Controls[0] is TextBox)<br /> {<br /> txtValues.Add(((TextBox)cell.Controls[0]).Text);<br /> }<br /> }<br /> }<br /><br /> newViewState[0] = txtValues.ToArray(); <br /> newViewState[1] = base.SaveViewState();<br /> return newViewState;<br /> }<br />protected override void LoadViewState(object savedState)<br /> {<br /> //if we can identify the custom view state as defined in the override for SaveViewState<br /> if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[] )<br /> {<br /> object[] newViewState = (object[])savedState;<br /> string[] txtValues = (string[])(newViewState[0]);<br /> if (txtValues.Length > 0)<br /> { <br /> //re-load tables<br /> CreateDynamicTable();<br /> int i = 0;<br /> foreach (TableRow row in tbl.Controls)<br /> {<br /> foreach (TableCell cell in row.Controls)<br /> {<br /> if (cell.Controls[0] is TextBox && i < txtValues.Length)<br /> {<br /> ((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();<br /><br /> }<br /> }<br /> }<br /> }<br /> //load the ViewState normally<br /> base.LoadViewState(newViewState[1]);<br /> }<br /> else<br /> {<br /> base.LoadViewState(savedState);<br /> }<br /> }<br />
Hurray.. Now you can access dynamic control value on button click.
Subscribe to post feed