在 ASP.NET 网页中使用控件集合

更新:2007 年 11 月

Control 类及其派生类(包括 Page 类)会公开一个可返回 ControlCollection 实例的 Controls 属性。通过这种层次结构,可以通过编程方式对控件树进行遍历以搜索某页上的特定控件,并检查集合中的控件类型,以便访问其属性。下面的代码示例演示如何通过遍历该页的控件层次结构来查找 <asp:TextBox> 控件的实例(只有一个)。

20zys56y.alert_security(zh-cn,VS.90).gif安全说明:

该示例具有一个文本框,用于接受用户输入,这是一个潜在的安全威胁。默认情况下,ASP.NET 网页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本侵入概述

<%@ 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>

请参见

其他资源

以编程方式访问 ASP.NET 控件