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

推荐订阅源

T
Tailwind CSS Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
WordPress大学
WordPress大学
Jina AI
Jina AI
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
有赞技术团队
有赞技术团队
V
Vulnerabilities – Threatpost
博客园 - 司徒正美
L
Lohrmann on Cybersecurity
P
Privacy International News Feed
S
SegmentFault 最新的问题
C
Cisco Blogs
V
V2EX
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
I
Intezer
人人都是产品经理
人人都是产品经理
博客园_首页
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cyberwarzone
Cyberwarzone
Know Your Adversary
Know Your Adversary
酷 壳 – CoolShell
酷 壳 – CoolShell
Security Latest
Security Latest
V
Visual Studio Blog
S
Schneier on Security
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - Franky
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
K
Kaspersky official blog
T
The Exploit Database - CXSecurity.com
G
GRAHAM CLULEY
量子位
S
Securelist
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tenable Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
T
Troy Hunt's Blog

博客园 - 小白天地

从今日起重新开张!!! 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页面的组成及控件的层次结构,通过递归实现控件遍历。