c# asp.net How to return a usercontrol from a handeler ashx?
Posted
by Justin808
on Stack Overflow
See other posts from Stack Overflow
or by Justin808
Published on 2010-06-08T23:55:28Z
Indexed on
2010/06/09
0:02 UTC
Read the original article
Hit count: 613
I want to return the HTML output of the control from a handler. My code looks like this:
<%@ WebHandler Language="C#" Class="PopupCalendar" %>
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?
© Stack Overflow or respective owner