Dynamically Created Controls in ASP.NET

I was working on a project last week that involved dynamically creating a bunch of controls on an ASP.NET page, then trying to, um, do stuff with them. (For lack of a better explanation…)

This article does a good job of explaining something that I’d kind of missed at first: dynamically-created controls don’t stay on a page after postback, normally. The “Page” class is stateless, destroyed after rendering the page. I didn’t wind up using this guy’s solution; I actually realized that I didn’t need to be dynamically creating the controls at all. I’d copied my page from another page that *did* need to create controls dynamically, but after I looked at my own page, I realized I just didn’t need to do things that way.

I’ve been doing more ASP.NET programming than usual lately, because I’m trying to finish up a project for a big client at work, and it’s kind of tricky and time-sensitive. Because of that, I’m just doing it (mostly) myself instead of farming it out to one of the other programmers. I’m having some fun with it. Aside from the dynamically created controls, I’m also playing around with generics a bit. This article by Jesse Liberty was helpful for setting up a simple collection using generics.

Leave a Reply