7

I am trying one more time to reach out to asp.net experts and hoping to get an answer. I am really stuck here and asking for help. Hopefully, my question will not get down voted, and I could get an answer purely from technical point of view instead of people simply being judgmental on my approach.

Earlier I posted question as follows: asp.net convert asp.net page into Page variable

Then I looked at following page but still its not working for me.

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

Inside my web application, I would like to be able to reference web pages any where in my code like "WebForm1.aspx" and get a listing of the controls on that page. Please just look at it from this point of view and not over analyze it. Is it possible?

In my Page variable p, I do not seem to have any controls for WebForm1.aspx

Here is my code.

Please help.

 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/>");
                }


            }
        }
Community
  • 1
  • 1
dotnet-practitioner
  • 13,968
  • 36
  • 127
  • 200

2 Answers2

4

Because of the ASP.NET Page Life Cycle, the controls are only created by processing the request (IHttpHandler.ProcessRequest(HttpContext)).

Before iterate over the controls you need to run the code below:

//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);

Below is the full code for 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>

Solution with the complete sample code: q_11423280WebApp.7z

Hailton
  • 1,192
  • 9
  • 14
2

Here's an alternative to loading the controls:

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
}
Candide
  • 30,469
  • 8
  • 53
  • 60
  • Thanks Ingenu!!!! is there a way that I can get away by not casting to Default on the 3rd line? This way I could go through different pages automatically. For now, the following line will not work if I want to iterate through different pages: var page = (Default)Activator.CreateInstance(type); – dotnet-practitioner Jul 11 '12 at 16:55
  • I see no reason why not: `var page = (Page)Activator.CreateInstance(type);` works just as well because `Default` inherits from `Page`. – Candide Jul 11 '12 at 17:22