Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Solve Dynamic Control Problem With PostBack

0.00/5 (No votes)
29 Mar 2007 1  
Create Dynamic Controls And Keep It On Page After PostBack

Introduction

When I try to create dynamic controls in gridview like (label, textbox, checkbox, etc.) lose my controls when the page is posted back. When I tried to find a solution on the internet I didn't find any so I decided to solve it by myself.

Problem Definition

  1. First I add TemplateColumn To gridview
  2. Then I but in (ItemTemplate) PlaceHolder control:
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" 
    AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" 
    OnDataBinding="GridView1_DataBinding" OnDataBound="GridView1_DataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RoomID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" 
    OnPreRender="PlaceHolder1_PreRender"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Then the code that will create the controls based in DataBase Filed (RoomID) in the event RowDataBound as follows:

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e) { 
PlaceHolder plc = (PlaceHolder)e.Row.Cells[1].FindControl("PlaceHolder1"); 
Label lbl_RoomID = (Label)e.Row.Cells[0].FindControl("Label1"); 
Label lblinsert = new Label();
        CheckBox chk = new CheckBox();
        TextBox txt = new TextBox();
        chk.Text = "tttttttt";
        lblinsert.Text = "xxxxxxxxx";
        
        if (lbl != null)
        {
            switch(lbl.Text)
            {
                case "1":plc.Controls.Add(lblinsert);
                        arr.Add(lblinsert);break;
                case "2":plc.Controls.Add(chk);
                        arr.Add(chk);break;
                case "3": plc.Controls.Add(txt);
                        arr.Add(txt); break;
                default: lblinsert.Text = " Dummy ";
                        plc.Controls.Add(lblinsert);
                        arr.Add(lblinsert); break;
               
            }
        }
} 
}

The Solution

When I creat controls in event RowDataBound I add them to ArrayList then at event DataBound I but ArrayList In Session

protected void GridView1_DataBound(object sender, EventArgs e)
{
Session.Add("arrcon", arr);
}

Then at the PlaceHolder_PreRender event I got ArrayList and recreate controls into it again with its data

Int count = 0 ;
protected void PlaceHolder1_PreRender(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            arr = (ArrayList)Session["arrcon"];
            if (arr != null)
            {

                if (arr[count] is TextBox)
                {
                    TextBox txt = (TextBox)arr[count];
                    txt.Text = Request.Form[txt.UniqueID].ToString();
                    ((PlaceHolder)this.GridView1.Rows[count].Cells[1].FindControl(
                        "PlaceHolder1")).Controls.Add(txt);
                }
                else if (arr[count] is Label)
                {                
                    ((PlaceHolder)this.GridView1.Rows[count].Cells[1].FindControl(
                        "PlaceHolder1")).Controls.Add((Control)arr[count]);

                }
                else if (arr[count] is CheckBox)
                {
                    CheckBox chk = (CheckBox)arr[count];
                    if (Request.Form[chk.UniqueID] != null)
                    {
                        chk.Checked = true;
                    }
                    else
                    {
                        chk.Checked = false;
                    }
                    ((PlaceHolder)this.GridView1.Rows[count].Cells[1].FindControl(
                        "PlaceHolder1")).Controls.Add(chk);
                }
               count++;
            }
        }
    }

And this solved my problem

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here