Suja Shyam
Suja Shyam

Reputation: 971

Jquery load usercontrol using generic handler

I need to load usercontrol using jquery. I created a handler class and loaded usercontrol through that. The jquery part is:

$.ajax({
    type: "POST",
    url: "HandlerAdminManageDataSideMenu.ashx",
    contentType: "application/html; charset=utf-8",
    dataType: "html",
    success: function (data) {
        alert("success" + data);
        $(".admin_side_menu").append(data);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        debugger;
        alert("Error Occured!");
    }
});

My handler file looks like :

Public Class HandlerAdminManageDataSideMenu
    Implements System.Web.IHttpHandler, IRequiresSessionState

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        'context.Response.ContentType = "text/plain"
        'context.Response.Write("Hello World!")

        context.Response.ContentType = "text/html"
        context.Response.Write(RenderPartialToString("Shared\AdminManageDataSideMenu.ascx"))
    End Sub

    Private Function RenderPartialToString(ByVal controlName As String) As String
        Dim page As New Page()
        Dim control As Control = page.LoadControl(controlName)
        page.Controls.Add(control)

        Dim writer As New StringWriter()
        HttpContext.Current.Server.Execute(page, writer, False)

        Return writer.ToString()
    End Function

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class

The line HttpContext.Current.Server.Execute(page, writer, False) throws error:

Error executing child request for handler 'System.Web.UI.Page'

Can anyone help me on this?

Upvotes: 1

Views: 2068

Answers (2)

Suja Shyam
Suja Shyam

Reputation: 971

Made slight changes to RenderPartialToString function:

    Private Function RenderPartialToString(ByVal controlName As String) As String

    Dim page As New Page()
    Dim usercontrol As UserControl = CType(page.LoadControl(controlName), UserControl)

    usercontrol.EnableViewState = False
    Dim form As HtmlForm = New HtmlForm()
    form.Controls.Add(usercontrol)
    page.Controls.Add(form)

    Dim textWriter As StringWriter = New StringWriter()
    HttpContext.Current.Server.Execute(page, textWriter, False)
    Return textWriter.ToString()

End Function

Reference link : http://www.aspxtutorial.com/post/2011/01/02/Load-aspnet-web-user-control-using-jQuery-and-web-method.aspx

Thanks for all responses.

Upvotes: 1

LostInComputer
LostInComputer

Reputation: 15430

Yes can use .load() instead. http://api.jquery.com/load/

"Description: Load data from the server and place the returned HTML into the matched element."

Sample from the link I gave to you:

$("#success").load("/not-here.php", function(response, status, xhr) {
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $("#error").html(msg + xhr.status + " " + xhr.statusText);
  }
});

Upvotes: 0

Related Questions