Search code examples
c#asp.netupdatepanel

Get the value of dynamically added text box from table inside update panel


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.


Solution

  • 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.