How do I get a custom IHttpHandler to Be Capable of Handling Postbacks?

痞子三分冷 提交于 2019-12-12 01:17:42

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/1093405/how-do-i-get-a-custom-ihttphandler-to-be-capable-of-handling-postbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!