共用方式為


使用控制項集合 ASP.NET Web 網頁

更新:2007 年 11 月

Control 類別 (Class) 和其衍生類別 (包含 Page 類別) 會公開 (Expose) 傳回 ControlCollection 執行個體 (Instance) 的 Controls 屬性。這個階層架構能夠讓您以程式設計方式,查核控制項樹狀結構以搜尋網頁上的特定控制項,並且檢查集合中的控制項型別以存取其屬性。下列程式碼範例示範如何逐步完成網頁的控制項階層架構,以便尋找 <asp:TextBox> 控制項 (其中僅有一個) 的執行個體。

安全性注意事項:

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。ASP.NET Web 網頁預設會驗證使用者輸入,但不包含當中的指令碼或 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" >

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

    <script language="vb" >

        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" >
    <table width="80%"
           border="1" 
           cellpadding="1" 
           cellspacing="1">
      <tr>
        <td align="center" style="width:50%;">
        <asp:TextBox id="MyTextBox" 
                     text="Type something here" 
                     />
        </td>
        <td align="center" style="width:50%;">
        <span id="myspan" >&nbsp;</span>
        </td>
      </tr>

      <tr>
        <td colspan="2" align="center">
        <input id="changebtn"
               type="submit"  
               onserverclick="changebtn_click" 
               value="move your text"
                />
        </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" >
    <title>Using the Controls Collection in a Web Form</title>

<script language="c#" >

  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" >
    <table width="80%"
           border="1" 
           cellpadding="1" 
           cellspacing="1">
      <tr>
        <td align="center" style="width:50%;">
        <asp:TextBox id="MyTextBox" 
                     text="Type something here" 
                     />
        </td>
        <td align="center" style="width:50%;">
        <span id="myspan" >&nbsp;</span>
        </td>
      </tr>

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

請參閱

其他資源

以程式設計方式存取 ASP.NET 控制項