Dan Monego
Dan Monego

Reputation: 10087

Handling dynamically generated controls in asp.net

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.

Upvotes: 1

Views: 317

Answers (3)

Astra
Astra

Reputation: 11211

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.

Upvotes: 0

Josh
Josh

Reputation: 44906

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.

Upvotes: 1

redsquare
redsquare

Reputation: 78667

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.

Upvotes: 3

Related Questions