zoukankan      html  css  js  c++  java
  • 遍历页面控件

    普通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(编号:) 
  • 相关阅读:
    手贱!使用django,在数据库直接删除了表
    js中在一个函数中引用另一个函数中的函数,可以这么做
    上传下载文件方式
    阻止form提交数据,通过ajax等上传数据
    一种思路,隐藏input标签,通过label关联
    java 寻找水仙花数
    java 统计素数个数问题
    java 兔子生仔问题
    java 实现读取某个目录下指定类型的文件
    通过java 来实现对多个文件的内容合并到一个文件中
  • 原文地址:https://www.cnblogs.com/_dragon/p/1656286.html
Copyright © 2011-2022 走看看