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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - sooloo

[转]Creating Custom Web Controls in C# Stats(演示了如何创建一个导航条) [转]创建动态数据输入用户界面(ASP.NET 中的动态控件入门) [转]ASP.NET 页面对象模型 [转]基于功能更丰富的基础类构建您自己的 ASP.NET 页面 [转]ASP.NET中促进代码重用的2种模式 [原创]利用CSS实现页面换肤 [转] 动态加载Asp.net分页控件 [转] more than one way to skin an app [转]模拟Asp.Net Forums实现可以换皮肤的控件 [转]在ASP.Net中两种利用CSS实现多界面的方法 DotText分析---新Blog注册 转:dotext数据库篇 Vs2003调试DotText时碰到的一个问题 DotText数据库分析(2) CnDotText数据库分析(1) 高程历年试题及答案 [转帖]追MM与设计模式 学习cnblogsGuestBook V2.0 --(2) 学习cnblogsGuestBook V2.0
asp.net遍历控件的实现
sooloo · 2005-10-01 · via 博客园 - sooloo

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页面的组成及控件的层次结构,通过递归实现控件遍历。