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:
<script runat="server" language="C#">
void Page_Load(Object sender, EventArgs e)
{
foreach(Control c in Controls)
lblControlList.Text += c.ToString() + " - " + c.ID + "<br>";
}
</script>
<html>
<head>
</head>
<body>
<b>A List of the Controls in the
<code>Controls</code> Collection</b><br>
<asp:label runat="server" id="lblControlList" />
<p>
<form runat="server">
What's your name?
<asp:textbox runat="Server" id="txtName" />
</form>
</body>
</html>
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.
<script runat="server" language="C#">
void IterateThroughChildren(Control parent)
{
foreach (Control c in parent.Controls)
{
lblControlList.Text += "<li>" + c.ToString() + "</li>";
if (c.Controls.Count > 0)
{
lblControlList.Text += "<ul>";
IterateThroughChildren(c);
lblControlList.Text += "</ul>";
}
}
}
void Page_Load(Object sender, EventArgs e)
{
lblControlList.Text += "<ul>";
IterateThroughChildren(this);
lblControlList.Text += "</ul>";
}
</script>
<html>
<head>
</head>
<body>
<b>A List of the Controls in the
<code>Controls</code> Collection</b><br>
<asp:label runat="server" id="lblControlList" />
<p>
<form runat="server">
What's your name?
<asp:textbox runat="Server" id="txtName" />
</form>
</body>
</html>
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.
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:
Working with Dynamically Created Controls
Looks at how to find a particular dynamically created control, and how to set/read its properties. Also
examines how to iterate through all dynamically created controls on a page.
Dynamic Web Controls, Postbacks, and View State
Examines using dynamic Web controls that can fully participate in the standard page lifecycle. Shows when to
add controls programmatically so that their view state and values are persisted correctly across postback.