【问题标题】:Identify Label and Button Control in each page of the web project识别 web 项目每个页面中的 Label 和 Button Control
【发布时间】:2012-07-11 08:44:48
【问题描述】:

我正在尝试再次联系 asp.net 专家并希望得到答案。我真的被困在这里并寻求帮助。希望我的问题不会被否决,我可以纯粹从技术角度得到答案,而不是人们简单地对我的方法进行评判。

之前我发布的问题如下: asp.net convert asp.net page into Page variable

然后我查看了以下页面,但它仍然不适合我。

Load an ASP.NET 2.0 aspx page using System.Reflection?

在我的 Web 应用程序中,我希望能够在我的代码中的任何位置引用网页,例如“WebForm1.aspx”,并获得该页面上的控件列表。请从这个角度来看,不要过度分析。可能吗?

在我的页面变量 p 中,我似乎没有任何用于 WebForm1.aspx 的控件

这是我的代码。

请帮忙。

 protected void Page_Load(object sender, EventArgs e)
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/"), "*.*", SearchOption.AllDirectories);

        foreach (string filepath in filePaths)
        {
            if (filepath.EndsWith(".aspx"))
            {
                Response.Write(filepath + "<br/>");

                string[] folders = filepath.Split('\\');
                string filename = folders[folders.Count() - 1];
                string fullpath = "~/" + filename;

                Page p = BuildManager.CreateInstanceFromVirtualPath("~/"+fullpath, typeof(Page)) as Page;

                List<String> controlList = new List<String>();
                ResourceManager.AddControls(p.Controls, controlList);

                foreach (string str in controlList)
                {
                    Response.Write(str + "<br/>");
                }


            }
        }

【问题讨论】:

  • 应用是asp.net网站还是web应用?
  • 网络应用程序.. 谢谢
  • 你试过了吗,YourPageType p = BuildManager.CreateInstanceFromVirtualPath("~/webform1.aspx", typeof(YourPageType)) as YourPageType;
  • 我不知道你的YourPageType是什么意思。我尝试了以下..但它没有工作。感谢您的反馈。 WebForm2 p = BuildManager.CreateInstanceFromVirtualPath("~/webform2.aspx", typeof(WebForm2)) as WebForm2;
  • Ingenu,你早期的帖子工作得很好......除了......我需要一个稍微不同的解决方案......有没有办法让页面没有类型转换为(默认)?再次感谢..

标签: c# asp.net


【解决方案1】:

由于ASP.NET Page Life Cycle,控件只能通过处理请求(IHttpHandler.ProcessRequest(HttpContext))来创建。

在遍历控件之前,您需要运行以下代码:

//this is necessary, Otherwise "Default.aspx" will show the contents of "WebForm1.aspx".
HttpWorkerRequest hwr = new SimpleWorkerRequest(this.TxtPageVirtualPath.Text, "", tw);
HttpContext fakeContext = new HttpContext(hwr);
((IHttpHandler)p).ProcessRequest(fakeContext);

下面是 Default.aspx 的完整代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Compilation;
using System.Collections.Generic;
using System.Resources;
using System.IO;
using System.Web.Hosting;

namespace _1423280WebApp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnLoad_Click(object sender, EventArgs e)
        {
            Page p = BuildManager.CreateInstanceFromVirtualPath(this.TxtPageVirtualPath.Text, typeof(Page)) as Page;

            List<String> controlList = new List<String>();

            MemoryStream ms = new MemoryStream();
            TextWriter tw = new StreamWriter(ms);
            HtmlTextWriter htw = new HtmlTextWriter(tw);

            //this is necessary, Otherwise "Default.aspx" will show the contents of "WebForm1.aspx".
            HttpWorkerRequest hwr = new SimpleWorkerRequest(this.TxtPageVirtualPath.Text, "", tw);
            HttpContext fakeContext = new HttpContext(hwr);

            ((IHttpHandler)p).ProcessRequest(fakeContext);

            //I could not compile this part in VS2005
            //ResourceManager.AddControls(p.Controls, controlList);


            this.TxtListControls.Text = "";
            foreach (Control ctr in p.Controls)
            {
                this.TxtListControls.Text += this.recursiveControls(p, "");
            }
        }

        public string recursiveControls(Control control, string ident)
        {
            string retStr = 
                String.Format(
                    ident + "D='{0}', ClientID='{1}', Type=='{2}' \n",
                    control.ID,
                    control.ClientID,
                    control.GetType().FullName);
            foreach (Control innerCtr in control.Controls)
            {
                retStr += this.recursiveControls(innerCtr, " " + ident);
            }

            return retStr;
        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_1423280WebApp._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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Get List of Controls from:<br />
        <asp:TextBox ID="TxtPageVirtualPath" runat="server">~/webform1.aspx</asp:TextBox><br />
        <asp:Button ID="BtnLoad" runat="server" OnClick="BtnLoad_Click" Text="Load" /><br />
        Controls:<br />
        <asp:TextBox ID="TxtListControls" runat="server" Height="328px" TextMode="MultiLine"
            Width="100%"></asp:TextBox></div>
    </form>
</body>
</html>

带有完整示例代码的解决方案:q_11423280WebApp.7z

【讨论】:

    【解决方案2】:

    这是加载控件的替代方法:

    protected void Page_Load(object sender, EventArgs e)
    {
        Type type = BuildManager.GetCompiledType("~/Default.aspx");
        var page = (Default)Activator.CreateInstance(type);
        ((IHttpHandler)page).ProcessRequest(HttpContext.Current);
        var count = page.Controls.Count;
        Response.Clear(); // Because we use HttpContext.Current the response has a lot of stuff
    }
    

    【讨论】:

    • 感谢 Ingenu!!!!有没有一种方法可以通过不在第三行转换为 Default 来逃脱?这样我就可以自动浏览不同的页面。现在,如果我想遍历不同的页面,以下行将不起作用: var page = (Default)Activator.CreateInstance(type);
    • 我看不出有什么理由不这样做:var page = (Page)Activator.CreateInstance(type); 也能正常工作,因为 Default 继承自 Page
    猜你喜欢
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多