tags:

views:

173

answers:

3

What's the best way to handle data entered through dynamically generated controls in ASP.NET?

Currently, I have a group of controls that are generated in the Page_Load stage, and I need to access the data from them. It seems like it might be possible to just use a hidden field that's read and parsed on postback, but I'd like to know if there's a better way to do it that makes better use of the framework.

+3  A: 

The key is recreating the controls on postback.

Old but good article to explain how and why.

You can also use the request.form collection to grab the posted values.

redsquare
forgot to mention, thank god for mvc!
redsquare
The short version of this is that Page_Load is too late in the game to create the controls: do it in Page_Init instead.
Joel Coehoorn
A: 

redsquare is right. If you can just remember that ASP.Net is a lie wrapped around Http then you will be ok. The browser doesn't really care that MS neatly abstracts the Http Request/Response away from you in the form of Web Controls. It only knows that it needs to wrap up each of the form variables and their respective values and send them back to the server via Http.

Josh
A: 

You want to keep the dynamic controls created to a minimum, ESPECIALLY if you are attaching events to them.

I am with @redsquare on the ASP.NET MVC recommendation.

Redbeard 0x0A