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