欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

遍历页面控件

发布时间:2025/6/17 编程问答 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 遍历页面控件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

普通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(编号:) 

 

 

 

 

 

转载于:https://www.cnblogs.com/_dragon/archive/2010/01/25/1656286.html

总结

以上是生活随笔为你收集整理的遍历页面控件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。