views:

36

answers:

2

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?

A: 

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).

R0MANARMY
A: 

Hi, sorry but are you conscious about what this line means?:

 context.Response.ContentType = "text/plain"; 

so everything you put out will only be plain text! not even html!

the handler has no code behind, you can not add a page and controls like that.

I think i see what you try to do:

Open the Calendar in a new BrowserWindow/Or make it look like a popup. For this you can use the ajax/jquery javascript libraries, you do not need a handler to implement a popup!

SwissCoder
Justin808
i did mean you want to create something looking like a popup.or why your name is popupcontrol?for what you create a handles, when all you want to do is display a control?
SwissCoder
im looking to have an on screen popup (in a div) calendar. I have a control that creates a calendar and would like to use it. I need to repopulate the div with a new month if the user wants to change the views month/year. Yes there are 10000 javascript controls, but the UserControl talks to a database and inactivates days / adds events / etc.I just find it odd that I can't take the HTML that would be generated by the control and dump it out to a string to pass back to ajax request.
Justin808