Working with Dynamically Created ControlsBy Scott Mitchell
In last week's article, Dynamic Controls in ASP.NET, I examined how to use the
Controlscollection to dynamically add a control to an ASP.NET Web page. Additionally, I illustrated how to enumerate through the
Controlscollection, both via a simple
For Each ... Nextloop and using recursion to completely enumerate through all of the controls on a page, as well their children, their children's children, and so on.
Since publishing that article there have been a number of readers who have asked roughly the same
question: when enumerating through the list of controls how can I set some of the controls properties.
Put another way, imagine that you wanted to find all TextBox Web controls and set their
property to some value, or perhaps you wanted to find the first DropDownList control and determine
Before beginning this article, make sure you have closely read Dynamic Controls in ASP.NET and understand the concepts presented in that article.
Finding a Particular Control by its Type
Controlclass contains a
GetType()method that returns a
Typeclass instance. The
ToString()method of the
Typeclass returns a string providing an English description of the type. For example, if in our
Page_Loadevent handler we had:
we will get the output:
System.Web.UI.WebControls.TextBox. Hence, if we want to search
for controls of a particular type, we can loop through all of our controls and check to see if the
current control we're examining has the type we're interested in. If it does, then we can take
whatever action we'd like to.
As an example, let's create an ASP.NET Web page that will dynamically create a user-specified number of TextBoxes, and then loop through all the controls, finding all of the TextBoxes. The HTML portion of our page might look like:
Essentially what we have here is a Web form with a TextBox asking the user to enter a value for the
number of TextBoxes that should be created dynamically. A RangeValidator is included to ensure that
the user enters an integer value between 1 and 10. A button then follows, which, when clicked, will
cause the form to be posted back and the
CreateTextBoxes event handler to fire. Finally,
TextBoxesHere PlaceHolder is the location where we'll place the dynamically created
The source code needs to include a
CreateTextBoxes event handler, which will create the
specified number of TextBoxes. This is fairly simple to implement, as we saw in the
Dynamic Controls in ASP.NET article.
In the first line, we read the number the user-entered number into a variable
then loop from 0 up to
n, creating a TextBox at each iteration, "attaching" the new TextBox
TextBoxesHere PlaceHolder. Finally, we call
This is the function we examined in Dynamic Controls in ASP.NET, which recursively iterates through all
of the controls on the page. It is within this function that we will be checking to see if the current
control we're enumerating through is a TextBox.
IterateThroughChildren steps through each control in the ASP.NET page. In our loop
Controls collection, we check to see if
the control is a TextBox by checking the
|Another Way to Determine the Control's Type...|
The code above gets the Control |
Thanks to Jarom K. and other alert 4Guys readers who wrote in to suggest this enhancement.
We also check to see if the
ID property is
null or not. The
ID is a
property of the
Control class, and, if specified, contains a string value. For example,
the TextBox where the user was asked to enter how many dynamic TextBoxes they wanted to create had an
txtTBCount. If no ID is explicitly specified (as with the
dynamically created TextBoxes, seeing as we didn't set the
ID property), the control's
ID property will be
The reason we check if the
null is because we only want to fiddle with
the dynamically created TextBoxes - that is, we don't want to alter the TextBox
Hence, we'll only enter the
if statement part if both the control's type is that of a
TextBox and the TextBox does not have an
ID specified. The only controls for which
these two conditions will hold true are those dynamically created TextBoxes.
At this point we know when we're dealing with a particular type of control, but once we find such a control, how do we tweak its properties? We'll look at how to accomplish this task in Part 2.