c# asp.net 如何从处理程序 ashx 返回用户控件?
我想从处理程序返回控件的 HTML 输出。我的代码如下所示:
使用系统; 使用系统.IO; 使用系统.Web; 使用 System.Web.UI; 使用 System.Web.UI.WebControls;
公共类 PopupCalendar : IHttpHandler {
public void ProcessRequest (HttpContext context) { context.Response.ContentType = "文本/纯文本"; System.Web.UI.Page 页面 = new System.Web.UI.Page(); UserControl ctrl = (UserControl)page.LoadControl("~/Controls/CalendarMonthView.ascx"); page.Form.Controls.Add(ctrl); StringWriter stringWriter = new StringWriter(); HtmlTextWriter tw = new HtmlTextWriter(stringWriter); ctrl.RenderControl(tw); context.Response.Write(stringWriter.ToString()); } 公共 bool IsReusable { 得到 { 返回假; } }
}
我收到错误:
“/CMS”应用程序中的服务器错误。 你调用的对象是空的。 描述:当前web执行期间发生未处理的异常。 要求。请查看堆栈跟踪以获取有关错误及其位置的更多信息 起源于代码。
异常详细信息:System.NullReferenceException:对象引用未设置为 对象的实例。
来源错误:
第 14 行:System.Web.UI.Page page = new System.Web.UI.Page(); 第15行:UserControl ctrl = (UserControl)page.LoadControl("~/Controls/CalendarMonthView.ascx"); 第 16 行:page.Form.Controls.Add(ctrl); 17号线:
第 18 行:StringWriter stringWriter = new StringWriter();
如何通过处理程序返回用户控件的输出?
I want to return the HTML output of the control from a handler. My code looks like this:
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public class PopupCalendar :
IHttpHandler {public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; System.Web.UI.Page page = new System.Web.UI.Page(); UserControl ctrl = (UserControl)page.LoadControl("~/Controls/CalendarMonthView.ascx"); page.Form.Controls.Add(ctrl); StringWriter stringWriter = new StringWriter(); HtmlTextWriter tw = new HtmlTextWriter(stringWriter); ctrl.RenderControl(tw); context.Response.Write(stringWriter.ToString()); } public bool IsReusable { get { return false; } }
}
I'm getting the error:
Server Error in '/CMS' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and where it
originated in the code.Exception Details: System.NullReferenceException: Object reference not set to an
instance of an object.Source Error:
Line 14: System.Web.UI.Page page = new System.Web.UI.Page();
Line 15: UserControl ctrl = (UserControl)page.LoadControl("~/Controls/CalendarMonthView.ascx");
Line 16: page.Form.Controls.Add(ctrl);
Line 17:
Line 18: StringWriter stringWriter = new StringWriter();
How can I return the output of a Usercontrol via a handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 BuildManager.GetCompiledType 方法加载具有所需内容的现有页面,使用接口实现设置一些公共属性,并调用其转换为 httphander 的 processrequest。通过这种方式,您不会直接公开 aspx 页面,并且可以为来自同一 ashx 资源的各种参数设计不同的页面。保留 asp.net 页面的优点和功能也很重要。
Use BuildManager.GetCompiledType method to load an existing page with desired content, set some public properties using interface implementation and call its processrequest casted into httphander. In this way you do not expose the aspx pages directly and you can deside different pages for various parameters from the same ashx resource. It is also important that you keep the benefits and power of asp.net pages.
你可以试试这个:
You can try this:
您不能仅仅
new
这样的页面类实例并使其正常工作。我相当确定您需要通过调用 GetCompiledPageInstance(基于此处的讨论)。You can't just
new
up an instance of a page class like that and have it properly work. I'm fairly certain you need to create it with a call to GetCompiledPageInstance (based on discussion here).