Click here to Skip to main content
Click here to Skip to main content

Solve Dynamic Control Problem With PostBack

, 29 Mar 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Create Dynamic Controls And Keep It On Page After PostBack


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">
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RoomID") %>'></asp:Label>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" 

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)
                case "1":plc.Controls.Add(lblinsert);
                case "2":plc.Controls.Add(chk);
                case "3": plc.Controls.Add(txt);
                        arr.Add(txt); break;
                default: lblinsert.Text = " Dummy ";
                        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();
                else if (arr[count] is Label)

                else if (arr[count] is CheckBox)
                    CheckBox chk = (CheckBox)arr[count];
                    if (Request.Form[chk.UniqueID] != null)
                        chk.Checked = true;
                        chk.Checked = false;

And this solved my problem


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Egypt Egypt
Team Leader For 3D game (1’s Person View) The Last Great Pharouh
Senior Instructor
Senior Developer .NET C#

Comments and Discussions

GeneralNice Article Pinmemberkumar_k50825-Apr-13 2:07 
GeneralMy vote of 4 Pinmemberralphy1018-Dec-10 0:04 
GeneralDynamic controls creation disappear problem PinmemberAsp bhai1-Mar-10 9:33 
Generalgr8, but I have very much moplicate datagrid...I can't use your solution... Pinmemberpintuuuuuuuuu17-Jun-08 4:11 
Questionok... but why use a session? Pinmembermorphix21-Mar-07 0:50 
AnswerRe: ok... but why use a session? Pinmembermagedo9321-Mar-07 5:30 
GeneralRe: ok... but why use a session? PinmemberNong Tien Bac20-Nov-08 19:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150302.1 | Last Updated 29 Mar 2007
Article Copyright 2007 by magedo93
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid