我想从处理程序返回控件的 HTML 输出。我的代码如下所示:
使用系统;使用 System.IO;使用 System.Web;使用 System.Web.UI;使用 System.Web.UI.WebControls;
公共类 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; } }
}
我收到错误:
“/CMS”应用程序中的服务器错误。你调用的对象是空的。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
源错误:
第 14 行:System.Web.UI.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();
如何通过处理程序返回用户控件的输出?