How To: Dynamically Load A Page For Processing

I have often desired the ability to dynamically load a page for processing, much like we do for .asxc controls with LoadControl(pathToASCX).  For example, I may want to create a custom HTTP handler or module to dynamically load a page - while allowing me to programmatically change key properties before processing.  Here is a method that accomplishes such a task:

public static void LoadPage(string pagePath)
{
    // get the compiled type of referenced path
    Type type = BuildManager.GetCompiledType(pagePath);

    // if type is null, could not determine page type
    if (type == null)
        throw new ApplicationException("Page " + pagePath + " not found");

    // cast page object (could also cast an interface instance as well)
    // in this example, ASP220Page is a custom base page
    ASP220Page pageView = (ASP220Page)Activator.CreateInstance(type);

    // call page title
    pageView.Title = "Dynamically loaded page...";
    // call custom property of ASP220Page
    pageView.InternalControls.Add(
        new LiteralControl("<hr />Served dynamically...</hr />"));            

    // process the request with updated object
    ((IHttpHandler)pageView).ProcessRequest(HttpContext.Current);
}
Published Friday, July 6, 2007 3:56 PM by Palermo4
Filed under: , , ,

Comments

# re: How To: Dynamically Load A Page For Processing

Friday, July 6, 2007 4:53 PM by Sean

Can you have the page (.aspx) and the code beside file (.aspx.cs) to be loaded dynamically or you have to have both in 1 file?... thanks.

# re: How To: Dynamically Load A Page For Processing

Friday, July 6, 2007 5:14 PM by Palermo4

Yes... this method works regardless of where the code resides.  It also functions regardless of using Master pages or not.  In my example, I just had to make sure that the code-behind file inherited from my custom base page - ASP220Page.

# re: How To: Dynamically Load A Page For Processing

Saturday, July 7, 2007 10:50 AM by Sean

My test failed. It complained about the Label control it couldn't find. the .aspx contained a single LAbel that was assigned a value from .aspx.cs file. I also was testing in a normal website project, not WAP. Any ideas?

# re: How To: Dynamically Load A Page For Processing

Saturday, July 7, 2007 10:53 AM by Sean

I am sorry, ignore my last message - I am too tired to think straight. It was an error I caused.

Thanks for the great tip.

# re: How To: Dynamically Load A Page For Processing

Saturday, July 7, 2007 10:56 AM by Dee

What's the advantage of this over a user control ??

# re: How To: Dynamically Load A Page For Processing

Saturday, July 7, 2007 11:01 AM by Sean

Details: while testing, I saw that the file has to be in the virtual folder on one of its sub-folders (since BuildManager.GetCompiledType() accepts only virtual path). IS there a way to load conent out of the virtual directory of the webapp?

Thanks

# re: How To: Dynamically Load A Page For Processing

Sunday, July 8, 2007 10:22 PM by Palermo4

Dee,

This really only makes sense when serving completely dynamic content.  This may be from a custom HTTP handler or module.

# re: How To: Dynamically Load A Page For Processing

Sunday, July 8, 2007 10:25 PM by Palermo4

Sean,

You are correct.  In this implementation, the page must exist in the web site.  There is another tip/trick on how to pull off another way, and I will blog about it sometime this month.

# re: How To: Dynamically Load A Page For Processing

Friday, August 17, 2007 4:06 PM by mike

This is a great piece of code.  The only problem that I have is that if property such as Title is already being set by the code behind or master page It will override any setting that I pass to the PageView object. So any munipulation I can do gets wiped out once ((IHttpHandler)pageView).ProcessRequest(HttpContext.Current) gets called.  Any help with this would be greatly appreciated.  I am trying to use this as a way to send to mock data to pages for Test Driven Development.

Thanks, Mike

# re: How To: Dynamically Load A Page For Processing

Friday, August 17, 2007 4:19 PM by Palermo4

mike,

Unfortunately, last modification wins.  The events that fire in the page will override your intitial value (as you have discovered)

To allow your assignment to Title to succeed, you would probably need to create a helper property or method on the base page that would manage the rules of which change wins.

# re: How To: Dynamically Load A Page For Processing

Tuesday, August 21, 2007 2:50 PM by mike

Palermo:  Thanks for your response.  I was able to put logic in the base class detects when there is a mock data source uses that data instead..

# re: How To: Dynamically Load A Page For Processing

Tuesday, August 21, 2007 3:32 PM by Mike

Did you ever figure out how to load conent out of the virtual directory of the webapp since BuildManager.GetCompiledType() accepts only virtual path?  I want to have a separate project for running tests against the web project. These tests will be executed by NUnit so it's better to have them in a separate assembly.  

Thanks,

Mike

# re: How To: Dynamically Load A Page For Processing

Thursday, August 30, 2007 11:28 AM by Mike

Anyone on this thread know of an alternative to BuildManager.GetCompiledType() that while allow me to programmatically load a page for processing from a separate assembly?  This assembly will contain automated tests against the web project in the same solution.  I need to be able to access the properties of the page and it's master page.. including server controls and their state. Currently I can do this inside of the web project but not in a separate assembly.

Thanks,

Mike

# re: How To: Dynamically Load A Page For Processing

Monday, October 15, 2007 9:29 AM by dario-g

Sean: I founded. :)

Look at System.Web.UI.PageParser.GetCompiledPageInstance(string virtualPath, string absolutePath, HttpContext context). :)

You can pass as virtualPath any string like "/page_name.aspx", as absolutePath a path to aspx file everywhere on hdd and current context. It works. :)

# re: How To: Dynamically Load A Page For Processing

Thursday, November 15, 2007 12:34 AM by john

compiledType = BuildManager.GetCompiledType(virtualPath);

if (compiledType == null)

 {

                           object page = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(System.Web.UI.Page));

                           compiledType = page.GetType();

 }

# re: How To: Dynamically Load A Page For Processing

Tuesday, March 11, 2008 7:29 AM by Kunal

How can we get the controls of the dynamically loaded page ?

# re: How To: Dynamically Load A Page For Processing

Friday, June 12, 2009 5:30 PM by ihsany

nice article..

# re: How To: Dynamically Load A Page For Processing

Tuesday, June 30, 2009 8:33 AM by Clothere

I have the same question as Kunal. I have an Admin page and would like to read the controls from another .aspx page.

# re: How To: Dynamically Load A Page For Processing

Thursday, June 3, 2010 11:54 AM by bespoke_web_developer

Thanks Palermo!

@Kunal:

in the case above you would do the following:

foreach (Control control in ((Page)pageView).Controls)

  control. .....

# re: How To: Dynamically Load A Page For Processing

Thursday, June 3, 2010 11:54 AM by bespoke_web_developer

Another question ...

what exactly does the following do:

((IHttpHandler)pageView).ProcessRequest(HttpContext.Current);

is this the same as a page redirect?

Leave a Comment

(required) 
(required) 
(optional)
(required)