Below are the controls in aspx.
<asp:UpdatePanel runat="server" ID="panel" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="holder"></asp:PlaceHolder>
<asp:Button runat="server" ID="bt" Text="Add" onclick="AddTxt" />
<asp:Button runat="server" ID="btn1" Text="Refresh" onclick="btn1_Click" />
</ContentTemplate>
When the button Add is clicked i am creating a dynamic table. Below is the C# code
protected void AddTxt(object sender, EventArgs e)
{
int tblRows = 3;
int tblCols = 3;
Table tbl = new Table();
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.TextChanged += new EventHandler(txt_TextChanged);
tc.Controls.Add(txtBox);
tr.Cells.Add(tc);
if (Session["ctls"] != null)
{
Session["ctls"] += ";t";
}
else
{
Session["ctls"] = "t";
}
}
tbl.Rows.Add(tr);
}
holder.Controls.Add(tbl);
panel.Update();
}
When the partial postback occurs on click of Refresh button i am not able to get the value which is updated by user inside the text box. The controls will have empty text.
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control c in holder.Controls)
{
if (c is TextBox)
{
}
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Session["ctls"] != null)
{
string[] ctls = Session["ctls"].ToString().Split(';');
foreach (string ctlType in ctls)
{
if (string.Compare(ctlType, "t") == 0)
{
holder.Controls.Add(new TextBox());
}
}
}
}
Could any one please help/give me a hint how to work around dynamic controls inside update panel.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The problem is that when you dynamically create the controls (table, rows, cells, textbox) in the AddTxt method you are assigning an ID value to the textbox, however when you dynamically create the controls in the OnInit event, then you are not creating ID value at all, thus the ViewState cannot be wired up to what the user typed in and thus lost.
If you create the textbox with the same ID value (txtBox.ID = "txt" + i.ToString() + j.ToString();) as in the other dynamic control creation logic, then your ViewState value would be preserved and properly applied to the textbox upon it being recreated in OnInit.
Note: ViewState is heavily dependent upon the ID values of server controls in order to properly associate values of controls between full and partial postbacks.
Method 2
Below is the corrected code, which works good now. I am adding this here because, it might help some one.
protected void AddTxt(object sender, EventArgs e)
{
add();
}
private void add()
{
int tblRows = 3;
int tblCols = 3;
Table tbl = new Table();
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.TextChanged += new EventHandler(txt_TextChanged);
tc.Controls.Add(txtBox);
tr.Cells.Add(tc);
}
tbl.Rows.Add(tr);
}
holder.Controls.Add(tbl);
if (Session["ctls"] != null)
{
Session["ctls"] += ";t";
}
else
{
Session["ctls"] = "t";
}
panel.Update();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Session["ctls"] != null)
{
string[] ctls = Session["ctls"].ToString().Split(';');
foreach (string ctlType in ctls)
{
if (string.Compare(ctlType, "t") == 0)
{
add();
}
}
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0