ASP.NET 页面控件遍历实现(转载+修改)

本文介绍了一种在ASP.NET中遍历页面上所有控件的方法,包括子控件,并展示了如何通过递归实现这一目标。此外,还提供了一个示例说明如何启用或禁用页面上的特定类型控件。
页面可以被看成各种控件组成的一个集合。在页面被初始化和加载过程中,可以遍历这些控件,找到特定的控件,或者改变某些控件的属性。
先看下面的一个例子:
     
script runat="server" language="C#">
  
void Page_Load(Object sender, EventArgs e)
  
{      
    
foreach(Control c in Controls)
       lblControlList.Text
+= c.ToString() + " - " + c.ID + "
";
   }

</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    

      为了真正做到遍历所有控件集,可以用递归的方法来实现:
<script runat="server" language="C#">
    
void IterateThroughChildren(Control parent)
    
{
      
foreach (Control c in parent.Controls)
      
{
         lblControlList.Text
+= "
" + c.ToString() + "";
        
if (c.Controls.Count > 0)       // 判断该控件是否有下属控件。
     
{
           lblControlList.Text
+= "
    ";
               IterateThroughChildren(c);    //递归,访问该控件的下属控件集。
               lblControlList.Text
    += "";
             }

           }

         }


        
    void Page_Load(Object sender, EventArgs e)
    {      
           lblControlList.Text
    += "
      ";
             IterateThroughChildren(
      this);
             lblControlList.Text
      += "";
           }

      </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
      • 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控件真的露出了庐山真面目。

                       

             ///
              /// 控制控件是否可用
              ///

              ///
              public static void EnableDisableOneControls(System.Web.UI.Control page, bool bFalg)
              {
                  int nPageControls = page.Controls.Count;
                  for (int i = 0; i < nPageControls; i++)
                  {
                      foreach (System.Web.UI.Control control in page.Controls[i].Controls)
                      {
                          if (control.HasControls())
                          {
                              EnableDisableOneControls(control, bFalg);
                          }
                          else
                          {
                              if (control is TextBox)
                                  (control as TextBox).Enabled = bFalg;

                              if (control is Button)
                                  (control as Button).Enabled = bFalg;

                              if (control is CheckBox)
                                  (control as CheckBox).Checked = bFalg;

                              if (control is DropDownList)
                                 
                                  (control as DropDownList).Enabled = bFalg;

                              if (control is UserControl)
                                  (control as UserControl).e = bFalg;

                              if (control is RadioButtonList)
                                  (control as RadioButtonList).SelectedIndex = -1;

                              if (control is RadioButton)
                                  (control as RadioButton).Checked = bFalg;

                              if (control is CheckBoxList)
                              {
                                  foreach (ListItem item in (control as CheckBoxList).Items)
                                  {
                                      item.Selected = bFalg;
                                  }
                              }
                          }//if..else
                      }//foreach
                  }//for
              }

      评论
      添加红包

      请填写红包祝福语或标题

      红包个数最小为10个

      红包金额最低5元

      当前余额3.43前往充值 >
      需支付:10.00
      成就一亿技术人!
      领取后你会自动成为博主和红包主的粉丝 规则
      hope_wisdom
      发出的红包
      实付
      使用余额支付
      点击重新获取
      扫码支付
      钱包余额 0

      抵扣说明:

      1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
      2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

      余额充值