惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
Security Latest
Security Latest
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
WordPress大学
WordPress大学
雷峰网
雷峰网
L
LangChain Blog
Y
Y Combinator Blog
I
Intezer
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
P
Privacy International News Feed
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
AI
AI
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
Martin Fowler
Martin Fowler
K
Kaspersky official blog
U
Unit 42
S
Schneier on Security
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
T
Tor Project blog
aimingoo的专栏
aimingoo的专栏
Engineering at Meta
Engineering at Meta
G
GRAHAM CLULEY
美团技术团队
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
P
Proofpoint News Feed
爱范儿
爱范儿
F
Fortinet All Blogs
有赞技术团队
有赞技术团队

博客园 - 小白天地

从今日起重新开张!!! D3D坐标系统和几何-DirectX Griaphic学习〔转载〕 重新开始我的blog内容! 关于Java栈与堆的思考[zz] MVC模式〔zz〕 google电话面试经历 面试英语_zt 指针和数组(C++)(zz) C#相关算法(zz) 某公司的一道机考题的解答[zz] 设计模式的点滴[zz] 30分钟学会STL [zz] STL实践指南[zz] PHP的HTML文字编辑器 [zz] C#数据库类(zz) C/C++的泪水![zz] 程序员数据结构笔记[zz] - 小白天地 - 博客园 百度之星程序设计大赛[zz] Asp.net与SQL一起打包部署安装[zz]
asp.net遍历控件的实现[zz] - 小白天地 - 博客园
小白天地 · 2005-10-05 · via 博客园 - 小白天地

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    

      为了真正做到遍历所有控件集,可以用递归的方法来实现:

<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

  • 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控件真的露出了庐山真面目。
      
3、动态地创建控件、遍历创建的控件以及改变控件属性的一个应用。
      用户输入一个1-10的整数,按按钮后动态创建相应数字的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>

 总结: 理解asp.net页面的组成及控件的层次结构,通过递归实现控件遍历。