Page_Load fired twice via ProcessRequest

P

pat.allan

Hi All

This seems to be a common problem here, but I've not found a solution
yet to fix it for me.

When I request a page through a browser, the page_load fires once. When
I request the page's code-behind object via a custom handler and the
code-behind object's ProcessRequest method, it fires twice - both times
from the ProcessRequest call, as far as I can tell.

I don't want to use PageParser, as I don't want the resulting HTML from
the entire page - what I want to do is access a custom control and just
get it's rendered output. A fair portion of my code is intepreting form
parameters and creating the code-behind object via reflection, but I'm
assuming that's not the cause of the problem.

Below is the code from the custom IHttpHandler's ProcessRequest method.
I've got the page's AutoEventWireup set to false, I have authentication
set to none, and I've no idea why this is happening. Nothing is pushed
through to Global's Application_Error, and the code-behind's PreRender
event only fires after both Page_Loads have occurred (So not everything
is duplicated?).

So if anyone can point out what I'm doing wrong, or why ProcessRequest
is calling the Page_Load twice and how to avoid it doing so, it would
be greatly appreciated.

Thanks all

Pat Allan

Code:

public void ProcessRequest(HttpContext context)
{
AppDomain tempDomain = AppDomain.CreateDomain("TempDomain",
AppDomain.CurrentDomain.Evidence,
AppDomain.CurrentDomain.BaseDirectory,
AppDomain.CurrentDomain.RelativeSearchPath,
AppDomain.CurrentDomain.ShadowCopyFiles);

NameValueCollection vars = null;
if (context.Request.HttpMethod.Equals("GET"))
vars = context.Request.QueryString;
if (context.Request.HttpMethod.Equals("POST"))
vars = context.Request.Form;

try
{
string path = context.Request.Url.AbsolutePath;
string obj = path.Substring(path.LastIndexOf("/")+1,
path.LastIndexOf(".") - (path.LastIndexOf("/")+1));

Assembly assembly = tempDomain.Load(vars["assembly"]);
object source = assembly.CreateInstance(obj);

((IHttpHandler)source).ProcessRequest(context);

Control ctl =
(Control)source.GetType().GetProperty(vars["ctl"]).GetValue(source,
null);
ctl.RenderControl(new HtmlTextWriter(context.Response.Output));
}
catch (Exception err)
{
context.Response.Write(err.Message);
}
finally
{
AppDomain.Unload(tempDomain);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top