asp.net遍歷控件

1、在頁面動態添加一個控件的方法。       在頁面的 HTML 代碼上設置一個 asp:PlaceHolder  站位控件,當頁面被加載的時候,在這個 PlaceHolder 控件上添加所需要的其他控件。

<asp:PlaceHolder runat="server" id="PutLabelHere" />
 Sub Page_Load(sender as Object, e as EventArgs)     sub Page_Load(sender as Object, e as EventArgs)       Dim lblMessage as New Label()       lblMessage.Text = "Hello, World!"       lblMessage.Font.Bold = True              PutLabelHere.Controls.Add(lblMessage)     end sub   End Sub

2、遍歷控件的方法。      頁面可以被看成各種控件組成的一個集合。在頁面被初始化和加載過程中,可以遍歷這些控件,找到特定的控件,或者改變某些控件的屬性。      先看下面的一個例子:

script runat="server" language="C#">   void Page_Load(Object sender, EventArgs e)   {           foreach(Control c in Controls)       lblControlList.Text += c.ToString() + " - " + c.ID + "<br>";   } </script> <html> <head> </head> <body>     <b>A List of the Controls in the      <code>Controls</code> Collection</b><br>     <asp:label runat="server" id="lblControlList" />     <p>     <form runat="server">         What's your name?         <asp:textbox runat="Server" id="txtName" />     </form> </body> </html>

      這個例子列出頁面上所有的控件,結果如下: A List of the Controls in the Controls Collection System.Web.UI.LiteralControl - System.Web.UI.WebControls.Label - lblControlList System.Web.UI.LiteralControl - System.Web.UI.HtmlControls.HtmlForm - System.Web.UI.ResourceBasedLiteralControl -       特別要注意的一點:以上代碼沒有列出 ID=“txtName”的 TextBox 控件!因爲這個 TextBox 控件包含在 Form 裏面,是 Form 的一個子控件。而我們的代碼 foreach(Control c in Controls) 只關心當前頁面 Controls 的控件,至於子控件卻未能涉及。(可以把這些控件理解成一個樹狀的層次關係)                                      頁面 Controls                                    /    |     /                         //foreach(Control c in Controls)                                控件1   控件2  控件3            // 只判斷控件1、2、3屬於頁面 Controls                                      /      /                          //而未涉及到下屬子控件                             子控件1   子控件2           爲了真正做到遍歷所有控件集,可以用遞歸的方法來實現:  總結: 理解 asp.net 頁面的組成及控件的層次結構,通過遞歸實現控件遍歷。

<script runat="server" language="C#">     void IterateThroughChildren(Control parent)     {       foreach (Control c in parent.Controls)       {         lblControlList.Text += "<li>" + c.ToString() + "</li>";         if (c.Controls.Count > 0)       // 判斷該控件是否有下屬控件。      {           lblControlList.Text += "<ul>";           IterateThroughChildren(c);    //遞歸,訪問該控件的下屬控件集。           lblControlList.Text += "</ul>";         }       }     }     void Page_Load(Object sender, EventArgs e) {             lblControlList.Text += "<ul>";       IterateThroughChildren(this);       lblControlList.Text += "</ul>";     } </script> <html> <head> </head> <body>     <b>A List of the Controls in the      <code>Controls</code> Collection</b><br>     <asp:label runat="server" id="lblControlList" />     <p>     <form runat="server">         What's your name?         <asp:textbox runat="Server" id="txtName" />     </form> </body> </html>

     以上代碼運行結果如下:

A List of the Controls in the Controls Collection        3、動態地創建控件、遍歷創建的控件以及改變控件屬性的一個應用。       用戶輸入一個1-10的整數,按按鈕後動態創建相應數字的 TextBox。代碼如下:

  • System.Web.UI.LiteralControl
  • System.Web.UI.WebControls.Label
  • System.Web.UI.LiteralControl
  • System.Web.UI.HtmlControls.HtmlForm
    •            System.Web.UI.LiteralControl
    •            System.Web.UI.WebControls.TextBox
    •            System.Web.UI.LiteralControl
  • System.Web.UI.ResourceBasedLiteralControl

      這下 TextBox 控件真的露出了廬山真面目。

script runat="server" language="C#">     int count = 1;          void IterateThroughChildren(Control parent)   //遍歷所有控件     {       foreach (Control c in parent.Controls)       {         if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"&&               c.ID == null)        //找到所有新創建的TextBox控件(新創建的 TextBox 的 ID 爲空,有別於頁面中id="txtTBCount"的TextBox)         {           ((TextBox) c).Text = "TextBox " + count.ToString(); //改變 TextBox 的屬性。           ((TextBox) c).Columns = 10;  //改變 TextBox 的屬性           count++;   //count爲全局變量,記錄找到的 TextBox 數量         }                  if (c.Controls.Count > 0)         {                     IterateThroughChildren(c);                   }       }     }     void CreateTextBoxes(Object sender, EventArgs e)  //按下按鈕激活的事件           if (!Page.IsValid) return;              int n = Int32.Parse(txtTBCount.Text);       //取得用戶輸入的數字。       //創建 n 個TextBox,並把它們加到 PlaceHolder 裏面        for (int i = 0; i < n; i++)       {         TextBoxesHere.Controls.Add(new TextBox());          }              //遍歷並設置每個 TextBox 的屬性       IterateThroughChildren(this);     } </script> <form runat="server">     How many TextBoxes would you like to create? (<i>Please choose vaule between 1 and 10</i>)<br />     <asp:textbox runat="Server" id="txtTBCount" Columns="3" />     <asp:RangeValidator runat="server" ControlToValidate="txtTBCount"              MinimumValue="1" MaximumValue="10" Type="Integer"              ErrorMessage="Make sure that you choose a value between 1 and 10!" />     <br />     <asp:button runat="server" Text="Create Dynamic TextBoxes"            OnClick="CreateTextBoxes" />     <p>     <asp:PlaceHolder runat="server" id="TextBoxesHere" /> </form>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章