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

推荐订阅源

B
Blog RSS Feed
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
H
Help Net Security
Recorded Future
Recorded Future
The Register - Security
The Register - Security
F
Full Disclosure
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
Security Archives - TechRepublic
Security Archives - TechRepublic
Simon Willison's Weblog
Simon Willison's Weblog
Cisco Talos Blog
Cisco Talos Blog
I
InfoQ
T
Tenable Blog
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
博客园 - 叶小钗
B
Blog
V
V2EX
Jina AI
Jina AI
L
LangChain Blog
月光博客
月光博客
W
WeLiveSecurity
U
Unit 42
AWS News Blog
AWS News Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 聂微东
V
Visual Studio Blog
A
Arctic Wolf
T
Tailwind CSS Blog
The Cloudflare Blog
SecWiki News
SecWiki News
S
SegmentFault 最新的问题
Hacker News - Newest:
Hacker News - Newest: "LLM"
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
www.infosecurity-magazine.com
www.infosecurity-magazine.com
腾讯CDC
雷峰网
雷峰网

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