To read the article online, visit

Working with Dynamically Created Controls

By Scott Mitchell


In last week's article, Dynamic Controls in ASP.NET, I examined how to use the Controls collection to dynamically add a control to an ASP.NET Web page. Additionally, I illustrated how to enumerate through the Controls collection, both via a simple For Each ... Next loop 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 Text property to some value, or perhaps you wanted to find the first DropDownList control and determine the SelectedItem.

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

The Control class contains a GetType() method that returns a Type class instance. The ToString() method of the Type class returns a string providing an English description of the type. For example, if in our Page_Load event handler we had:

Dim txtName as New TextBox()

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:

<form runat="server">
    How many TextBoxes would you like to create?
    <asp:textbox runat="Server" id="txtTBCount" Columns="3" />

    <asp:RangeValidator runat="server" ControlToValidate="txtTBCount"
           MinimumValue="1" MaximumValue="10" Type="Integer"
           ErrorMessage="Choose a value between 1 and 10" />

    <br />
    <asp:button runat="server" Text="Create Dynamic TextBoxes"
           OnClick="CreateTextBoxes" />

    <asp:PlaceHolder runat="server" id="TextBoxesHere" />

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, the TextBoxesHere PlaceHolder is the location where we'll place the dynamically created TextBoxes.

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.

void CreateTextBoxes(Object sender, EventArgs e)
  int n = Int32.Parse(txtTBCount.Text);
  // now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
  for (int i = 0; i < n; i++)
    TextBoxesHere.Controls.Add(new TextBox());
  // now, set the Text property of each TextBox

In the first line, we read the number the user-entered number into a variable n. We then loop from 0 up to n, creating a TextBox at each iteration, "attaching" the new TextBox to the TextBoxesHere PlaceHolder. Finally, we call IterateThroughChildren. 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.

void IterateThroughChildren(Control parent)
  foreach (Control c in parent.Controls)
    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")
          && c.ID == null)
      // something...
    if (c.Controls.Count > 0)

The IterateThroughChildren steps through each control in the ASP.NET page. In our loop through the parent control's Controls collection, we check to see if the control is a TextBox by checking the GetType().ToString() value.

Another Way to Determine the Control's Type...
The code above gets the Control c's type using GetType() and comparing its string value to "System.Web.UI.WebControls.TextBox". A better approach would be to use the is operator (or TypeOf, in Visual Basic). That is, we could replace c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") with c is TextBox (or, in VB, TypeOf c Is TextBox).

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 ID of 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 null.

The reason we check if the ID is null is because we only want to fiddle with the dynamically created TextBoxes - that is, we don't want to alter the TextBox txtTBCount. 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.

  • Read Part 2!

  • Article Information
    Article Title: ASP.NET.Working with Dynamically Created Controls
    Article Author: Scott Mitchell
    Published Date: August 21, 2002
    Article URL:

    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers