Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically created controls disappearing on postback

Status
Not open for further replies.

tcstom

Programmer
Aug 22, 2003
235
GB
Help! I have a web form that contains a placeholder. My code generates a series of web controls (textboxes, radiobuttonlists etc) and adds them to this placeholder one at a time. This works fine. A user then fills in the resulting HTML form and submits the form. This works fine and all the controls are still on the page after the postback has occurred. However, sometimes the user is loading a saved form which basically means that the form is built in the same way, but in addition an XML document is accessed and used to pre-populate some of the controls. If this is the case then clicking the same submit button loses all my dynamic controls. It is as though the placeholder has suddenly decided to disable its view state (although I've checked and this property remains set to true). I hope this makes sense because it is baffling me. The only programmatic difference between the two scenarios is that the latter one calls an extra method to pre-populate the controls. When the page has loaded it is effectively identical except that some form elements already contain data. Nothing else is different so why is the placeholder empty when the page is returned? It beats me. Anyone have any ideas? I wish I could give you some example code but the class is enormous I'm afraid...

Tom
emo_big_smile.gif
 
Where are you calling the code that creates the dynamic controls? When I have done pages like this, I create the controls in the Page_Init instead of the Page_Load.

Jim
 
The method will be called either from Page_Load or from a Button Click event depending on which scenario described above. It's when called from the Click event that I lose my buttons after a subsequent postback. Let me re-write the code so that the controls are always created on Page_Load to see if that makes any difference.

By the way, why would it make a difference where in the code the controls are created? Surely if the web form contains a control that remembers its view state then it should always do so no matter when or where it was created?

Tom
emo_big_smile.gif
 
It turned out that this was the problem. The dynmaic controls are now always created on Page_Load and this has solved my problem. Thanks for the pointer though jbenson001. Still, my question remains, why should it matter at what point in the code such controls are created??

Tom
emo_big_smile.gif
 
You can also override createchildcontrols to create your controls along with everything else. It might be overkill though, if the owner form is a webform and not a usercontrol.

Brian Begy
Chicago Data Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top