如何获得能够处理回发的自定义 IHttpHandler?

发布于 2024-07-26 12:06:00 字数 1252 浏览 4 评论 0原文

我正在编写一个自定义 HTTP 处理程序,以使用现有用户控件为网格提供编辑表单。 基本上,我的处理程序创建页面、表单、标题和其他必要的控件,以便能够正确呈现现有用户控件,并且在 ProcessRequest 结束时,我使用 Server.Execute 执行动态创建的页面。 我这样做是因为它所在的解决方案是一个用户控制项目,并且没有页面,我们也不能添加任何页面。 这需要可重复用于多个项目。

直到添加到此“页面”的用户控件需要使用回发机制为止,这都非常有效。 在用户控件中,Page.IsPostBack 始终为 false,并且不处理控件事件(如按钮单击)。 很明显,我遗漏了典型 ASP.NET 页面工作方式的一些关键部分。 Page 类只是 IHttpHandler 的实现,但我认为有很多代码不需要在这里实现基本功能。

有任何想法吗?

这是我的基本 HTTP 处理程序的基本代码。 我有其他类继承此基本处理程序,以将实际的用户控件添加到页面的表单中。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-08-02 12:06:01

为了实现这项工作,我从 Page 继承而不是实现 IHttpHandler。 您仍然需要构建页面的整个 HTML,但是当您这样做时,您将获得 ASP.NET WebForms 页面生命周期的所有精彩(或没有)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文