views:

321

answers:

1

I am writing a custom HTTP handler to provide an edit form for a grid using existing user controls. Basically, my handler creates the page, form, header, and other controls necessary to be able to render the existing user controls properly and at the end of ProcessRequest, I use Server.Execute to execute the page that was dynamically created. I am doing this because the solution where this resides is a user controls project and there are no pages, nor can we add any. This needs to be reusable for several projects.

This works great up until the point where the user controls added to this "page" require the usage of the postback mechanism. In the user control Page.IsPostBack is always false and control events (like a button click) are not handled. It is obvious that I am missing some critical piece from how a typical ASP.NET page works. The Page class is just an implementation of an IHttpHandler, but there is a lot of code that I don't think should be necessary to get the basic functionality to work here.

Any ideas?

Here's the basic code from my base HTTP handler. I have other classes that inherit from this base handler to add the actual user controls to the form of the page.

public void ProcessRequest(HttpContext context) {
        context.Response.ContentType = "text/html";

        HtmlGenericControl htmlPage = GetHtml();
        AddTitle();
        htmlPage.Controls.Add(_head);

        HtmlGenericControl htmlBody = GetBody();

        _form.Action = context.Request.Url.ToString();
        _form.Method = "POST";
        htmlBody.Controls.Add(_form);

        htmlPage.Controls.Add(htmlBody);

        AddAjaxManager();
        AddScriptManager();

        _page.Controls.Add(htmlPage);
        //_page.ProcessRequest(context);

        context.Response.CacheControl = "No-Cache";
        context.Server.Execute(_page, context.Response.Output, true);
    }

    public bool IsReusable { get { return false; } }
A: 

To make this work, I inherited from Page instead of implementing IHttpHandler. You do still need to build out the entire HTML of the page, but you get all the wonderfulness (or not) of the ASP.NET WebForms page lifecycle when you do this.

Sumo