There are a few things to keep in mind when you create controls on the fly and add them to your page. Simply put, you want to add them to the page hierarchy as soon as possible. Before they are added to the page, they have not initialized themselves. When a dynamic control is added to another control, the new control plays catch-up to get to the stage that the parent control is in. For instance, if in your Page_Load, you add a textbox, it will play catch-up and go through its Init and Load phases. This is important beceause it will start tracking its viewstate. Values added before it is tracking viewstate won’t make it to viewstate and will be lost on PostBack. Here is an example that won’t work:
TextBox txt = new TextBox();
if(!IsPostBack)
{
txt.Text = “test value”;
}
this.Form1.Controls.Add(txt);
This example shows setting a text property BEFORE this control is added to the page (and before it is tracking viewstate). Next, we’ll show the right way to do it:
this.Form1.Controls.Add(txt);
if(!IsPostBack)
{
txt.Text = “test value”;
}
Note here that we add the control to the page BEFORE we set a property. This enables our dynamic control to participate in viewstate as well as the rest of the page lifecycle.
To clarify, if we add a dynamic control during the Page’s PreRender, when we do the Controls.Add( ) function, the new control will go through Init, Load and PreRender immediately to catch up with its parent (the Page).
I was facing problem on retaining viewstate for a dynamic control in PlaceHolder.
Your tip works for me. Thank you very much
I was encountering this exact situation. Adding my dynamic control to the page before I set data – in my case, before I did a databind on my dynamic control – fixed my problem.
i tried to achieve same but still facing some problem. i tried to add a text box.I can see the value in dynamically added textbox eevn on postback but if try to assign that value to some other control i dun see that value. Can somebody help in this context.
Brilliant – I am facing a similar problem. I think u are the right person for that. Here is the problem.
I am having a PlaceHolder Control with properties EnableViewState as true. Now i am dynamically loading a custom Datagrid in it. This Datagrid is also having its viewstate as true. Now when i press the edit button in the datagrid, i am facing a Javascript Error "Expected ‘)’". On pressing ‘OK’ on this POP-UP window i get the Update and Cancel Image buttons(inside the dataGrid as desired) in place of Edit Button. On updating the data in the grid and pressing Update button, i am again facing this Javascript Error. Astonishing again on pressing ‘OK’ on this POP-UP i am getting the desired functionality(new viewstate is maintained).
Problem is this javascript error which is Interrupting me in between. I have tried my level best to find the root cause of this Error but in vain. I am looking forward for the kind assistance on this problem from the Control experts like u. Any help on this topic will be highly appreciated.
Looking forward for a prompt reply.
http://www.experts-exchange.com/Programming/Q_21422966.html
E-mail:
anuj.kapoor@capgemini.com
Two words – life saver !!! Thanks!
Brilliant – You solved my problem. Thanks!
Excellent point.
Nice and simple, thank you very much. By chance I got this working in a webform and couldn’t get it to work in another. Went through a lot of articles but none explanied this simple fact. As a plus, you explaind WHY it works like that. Thanks again.
very clever!