遍历页面控件

普通aspx页面:

页面所有元素
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Web._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Controls</title>
</head>
System.Web.UI.LiteralControl-
System.Web.UI.HtmlControls.HtmlHead-
System.Web.UI.LiteralControl-
System.Web.UI.HtmlControls.HtmlForm-form1
System.Web.UI.LiteralControl-

 


<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:Button ID="Button1" runat="server" Text="Button" />
        
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    
</div>
    
</form>
</body>
</html>

 

 

呈现最顶层控件元素代码

foreach (Control control in Page.Controls)
{
    Response.Write(control.GetType().ToString() 
+ "-<b>" + control.ID + "</b><br/>");
}

  

显示结果(不包含子控件)

System.Web.UI.LiteralControl-
System.Web.UI.HtmlControls.HtmlHead-
System.Web.UI.LiteralControl-
System.Web.UI.HtmlControls.HtmlForm-form1
System.Web.UI.LiteralControl-

 

 

取页面所有控件元素,包含子控件

取页面所有控件元素
        protected StringBuilder conInfo = new StringBuilder();

        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!Page.IsPostBack)
            {
                outputControl(Page.Controls, 
0);
            }

            Response.Write(conInfo.ToString());
        }
        
protected void outputControl(ControlCollection controls, int depth)
        {
            
foreach (Control control in controls)
            {
                conInfo.Append(
string.Format("<br/>{0}>"new string('-', depth * 4)));

                conInfo.Append(
string.Format("{0}(<b>编号:{1}</b>)", control.GetType().ToString(),control.ID));

                
if (control.Controls.Count>0&&control.Controls!=null)
                {
                    conInfo.Append(
string.Format("(拥有{0}个子控件)", control.Controls.Count));

                    outputControl(control.Controls, depth 
+ 1);
                }
            }   
        }

 

 

显示结果

呈现页面所有控件元素
>System.Web.UI.LiteralControl(编号:)
>System.Web.UI.HtmlControls.HtmlHead(编号:)(拥有1个子控件)
---->System.Web.UI.HtmlControls.HtmlTitle(编号:)
>System.Web.UI.LiteralControl(编号:)
>System.Web.UI.HtmlControls.HtmlForm(编号:form1)(拥有9个子控件)
---->System.Web.UI.LiteralControl(编号:)
---->System.Web.UI.WebControls.Button(编号:Button1)
---->System.Web.UI.LiteralControl(编号:)
---->System.Web.UI.WebControls.LinkButton(编号:LinkButton1)
---->System.Web.UI.LiteralControl(编号:)
---->System.Web.UI.WebControls.TextBox(编号:TextBox1)
---->System.Web.UI.LiteralControl(编号:)
---->System.Web.UI.WebControls.Label(编号:Label1)
---->System.Web.UI.LiteralControl(编号:)
>System.Web.UI.LiteralControl(编号:) 

 

 

 

 

 

posted @ 2010-01-25 22:50  ._  阅读(946)  评论(0编辑  收藏  举报