Share via


Using the Controls Collection in an ASP.NET Web Page

The Control class and its derived classes (including the Page class) expose a Controls property that returns a ControlCollection instance. This hierarchy allows you to walk the control tree programmatically to search for specific controls on a page, as well as to check the type of controls within the collection in order to access their properties. The following code example shows how to walk through the page's control hierarchy to find instances of the <asp:TextBox> control (of which there is only one).

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="VB"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="head1" runat="server">

    <title>Using the Controls Collection in a Web Form</title>

    <script language="vb" runat="server">

        Private Sub ChangeBtn_Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim c As Control
            Dim c2 As Control

            For Each c In Page.Controls
                If c.Controls.Count > 0 Then
                    For Each c2 In c.Controls
                        If c2.GetType.ToString = "System.Web.UI.WebControls.TextBox" Then
                            MySpan.InnerHtml = CType(c2, TextBox).Text
                            CType(c2, TextBox).Text = ""
                        End If
                    Next
                End If
            Next
        End Sub

</script>

</head>

<body>
  <form id="form1" runat="server">
    <table width="80%"
           border="1" 
           cellpadding="1" 
           cellspacing="1">
      <tr>
        <td align="center" style="width:50%;">
        <asp:TextBox id="MyTextBox" 
                     text="Type something here" 
                     runat="server"/>
        </td>
        <td align="center" style="width:50%;">
        <span id="myspan" runat="server">&nbsp;</span>
        </td>
      </tr>

      <tr>
        <td colspan="2" align="center">
        <input id="changebtn"
               type="submit"  
               onserverclick="changebtn_click" 
               value="move your text"
               runat="server" />
        </td>
      </tr>
    </table>

  </form>
</body>
</html>
<%@ Page Language="C#"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="head1" runat="server">
    <title>Using the Controls Collection in a Web Form</title>

<script language="c#" runat="server">

  private void ChangeBtn_Click(object sender, EventArgs e)
  {
     foreach(Control c in Page.Controls)
     {
       if (c.Controls.Count > 0)
       {
         foreach(Control c2 in c.Controls)
         {
            if (c2.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                myspan.InnerHtml = ((TextBox)c2).Text;
               ((TextBox)c2).Text = "";
            }
         }
      }
   }
}

</script>

</head>
<body>
  <form id="form1" runat="server">
    <table width="80%"
           border="1" 
           cellpadding="1" 
           cellspacing="1">
      <tr>
        <td align="center" style="width:50%;">
        <asp:TextBox id="MyTextBox" 
                     text="Type something here" 
                     runat="server"/>
        </td>
        <td align="center" style="width:50%;">
        <span id="myspan" runat="server">&nbsp;</span>
        </td>
      </tr>

      <tr>
        <td colspan="2" align="center">
        <input id="changebtn"
               type="submit"  
               onserverclick="ChangeBtn_Click" 
               value="move your text"
               runat="server" />
        </td>
      </tr>
    </table>

  </form>
</body>
</html>

See Also

Other Resources

Accessing ASP.NET Controls Programmatically