Dynamic Controls in ASP.NET, Part 2
By Scott Mitchell
In Part 1 we examined how to add controls to an ASP.NET Web page, and how to specify the precise location of the control using a PlaceHolder control. In this part we'll examine how to enumerate through the controls on a page and some real-world uses of dynamically adding controls to an ASP.NET Web page.
Enumerating through the Controls on an ASP.NET Web Page
Since the
Controls property is a collection that supports the IEnumerable
interface, you can simple iterate through the collection using a For Each ... Next loop
in VB.NET or a foreach loop in C#. (For the remainder of this article I will be using
C# code samples.) To loop through the controls in the Page, simply use the following code:
|
Take a moment to view the live demo. You'll note that the controls listed to be in the page are:
- LiteralControl
- Label
- LiteralControl
- HtmlForm
- ResourceBasedLiteralControl
You may be wondering where the TextBox control is, and why it isn't listed on the live demo.
This is because the TextBox is a child control of the Web Form control (the HtmlForm control). In order
to view all of the controls on the Web page, we must iterate through the Controls
collection, and at each control, check to see if the control has any children controls; if it does,
we need to iterate recursively through its children controls, applying the same logic. (In order to
determine if a control has children controls, we can simply check to see if the control's
Controls collection's Count property is greater than 0.)
| What is the ResourceBasedLiteralControl? |
|---|
I found myself wondering this very thing when I first viewed the live demo. There's no mention of a
ResourceBasedLiteralControl in the docs and a search on Google revealed nothing helpful.
I turned to the ASP.NET Forums and got an answer from
DmitryR, an ASP.NET Team member.
DmitryR said:
[The choice of using a ResourceBasedLiteralControl vs. a LieralControl is] based on size, it is a perf optimization -- static HTML of size over 1K(?) ends up as a utf-8 encoded resource, saving the conversion cost and string size. |
The following code example illustrates how to recursively iterate through all of the controls on the page. It utilizes recursion - if you are unfamiliar or rusty with recursion, I'd recommend that you read Recursion - Why It's Cool first.
|
Ok, this is Neat, but is it Useful?
Hopefully you've found the material that we've covered thus far to be interesting, if nothing else. But, if you're like me, when you first hear about this topic you may find yourself thinking, "Well, yeah, this is neat and all, but when in the world would I use it?" Personally I have yet to use it in a real-world application, but can envision a number of potential uses.
Imagine that you want to have surveys or questionnaires on your Web site. Ideally each survey would have a database record, and a set of rows indicating what the questions were and what types of questions they were. For example, you might have a survey that had three questions: one a Yes/No, one a write-in, and one being a selection from five potential options. (In this case you'd need a couple of DropDownLists and a TextBox.) In any event, rather than have to create a new ASP.NET Web page for each potential survey, you could create one generic page that accepts a survey ID through the querystring, and then dynamically adds the appropriate questions and controls accompanying the questions. See? A useful, real-world application of the material we just covered! ;-)
Conclusion
In this article we examined how to dynamically add controls to an ASP.NET Web page. Furthermore, we looked at how to iterate through the
Controls collection as well as how to recursively
iterate through the entire set of controls on a page. For more information on working with dynamically added controls,
be sure to read: Working with Dynamically Created Controls.
Happy Programming!
| For More Information on Working with Dynamically Created Controls... |
|---|
After you have read this article, consider reading the following articles for a more in-depth look at working with dynamically
created controls in an ASP.NET Web page:
|




