2012-01-03 6 views
1

jqueryを使用してusercontrolをロードする必要があります。私はハンドラクラスを作成し、それを通してusercontrolをロードしました。 jqueryの一部です:Jqueryジェネリックハンドラを使用してusercontrolをロードする

$.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!"); 
    } 
}); 

マイハンドラファイルは次のようになります。

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 

ラインHttpContext.Current.Server.Execute(page, writer, False)をエラーがスローされます:

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

誰もがこの上で私を助けることができますか?

+0

http://stackoverflow.com/a/491233/335105これがあなたの問題を解決するかどうかを確認してください – Rafay

+0

は解決策を見つけることができませんでした –

答えて

1

メイドわずかな変化:

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 

参考リンク:すべての応答のためのhttp://www.aspxtutorial.com/post/2011/01/02/Load-aspnet-web-user-control-using-jQuery-and-web-method.aspx

感謝。

0

はい、代わりに.load()を使用できます。 http://api.jquery.com/load/

"説明:サーバーからデータを読み込み、返されたHTMLを一致する要素に配置します。"私があなたに与えたのリンクから

サンプル:RenderPartialToString機能へ

$("#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); 
    } 
}); 
+0

Webページではなくusercontrolをロードする必要があります –

関連する問題