2011-01-04 3 views
6

jQuery로 ascx 파일을로드하는 방법이 있습니까?jQuery를 통해 ascx로드

UPDATE : @Emmett 및 @Yads에

감사합니다. 다음 jQuery 아약스 코드와 함께 처리기를 사용하고 있습니다.

jQuery.ajax({ 
    type: "POST", //GET 
    url: "Foo.ashx", 
    data: '{}', 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (response) 
    { 
     jQuery('#controlload').append(response.d); // or response 
    }, 
    error: function() 
    { 
     jQuery('#controlload').append('error'); 
    } 
}); 

그러나 오류가 발생합니다. 코드가 잘못 되었나요?

또 다른 업데이트 : 내가

error: function (xhr, ajaxOptions, thrownError) 
{ 
    jQuery('#controlload').append(thrownError); 
} 

을 사용하고이 내가 무엇을 얻을 수 있습니다 :

잘못된 JSON :
테스트 => (이 테스트 내 ASCX 내부 레이블)

오류가 발생한 후 내 ascx 파일!

또 다른 업데이트는 :

내 ASCX 파일은 다음과 같은 뭔가를해야만이다

<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true"> 
    <asp:ListItem>1</asp:ListItem> 
    <asp:ListItem>2</asp:ListItem> 
</asp:DropDownList> 
<asp:Label ID="Label1" runat="server">Test</asp:Label> 

하지만 아약스를 호출 할 때 내가 ASP에서이 오류를 얻을 : :(

제어 'ctl00_ddl을 '유형의'DropDownList '는 runat = server 형식 태그 안에 있어야합니다.

@ Yads 덕분에

덕분입니다. 하지만 그의 솔루션은 html 태그에서만 작동합니다.

+0

http://stackoverflow.com/questions/1212639/possible-to-load-ascx-with -jquery-load-function – Anders

+0

불가능합니까? ascx 파일을 aspx로 변환하는 방법이 있습니까? 또는 더 나은 방법? – Raika

+0

오류 함수는 요청 객체를 취할 수 있으며 request.responseText를 검사하여 오류의 원인을 찾습니다. – Vadim

답변

10

jQuery.ajax({ 
    type: "POST", //GET 
    url: "Foo.ashx", 
    dataType: "html", 
    success: function (response) 
    { 
     jQuery('#controlload').append(response); // or response 
    }, 
    error: function() 
    { 
     jQuery('#controlload').append('error'); 
    } 
}); 
+0

도 webforms에서이 작업을 수행하고 있습니까? – Raika

+0

@ Raika 예 그냥 가서 Add-> New Item-> Generic Handler – Vadim

+0

고마워요 죄송합니다 : D – Raika

1

* .ascx 파일은 클라이언트 쪽 (JavaScript가 실행되는 곳)이 아닌 서버 쪽 (* .aspx 페이지 내부)에 렌더링됩니다.

빈 * .aspx를 만들고 사용자 정의 컨트롤을 * .aspx 페이지에 놓은 다음 jQuery를 통해 해당 페이지를 가져 와서 페이지에 결과를 덤프하는 옵션이 있습니다.

편집

귀하의 의견을 바탕으로, 나는 다른 제안이 : 그들은 compatible with the ASP.NET AJAX Toolkit을되도록

당신이 CMS 스타일의 응용 프로그램을 개발하는 경우, 당신은 당신의 *의의 .ascx 컨트롤을 구축해야한다. 그러면 사용자는 완전히 새로 고침하지 않고 페이지에 콘텐츠를 추가 할 수 있습니다.

사용자가 실제로 멋지게 만들고 싶다면 Web Parts and ASP.NET AJAX을 확인해야합니다. 사용자가 자신의 페이지에서 콘텐츠를 사용자 지정할 수 있도록 웹 파트가 실제로 디자인 되었기 때문입니다.에 모트의 솔루션

public class FooHandler : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "text/html"; 
     context.Response.Write(RenderPartialToString("Foo.ascx")); 
    } 

    private string RenderPartialToString(string controlName) 
    { 
     Page page = new Page(); 
     Control control = page.LoadControl(controlName); 
     page.Controls.Add(control); 

     StringWriter writer = new StringWriter(); 
     HttpContext.Current.Server.Execute(page, writer, false); 

     return writer.ToString(); 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

사용하여 다음 JQuery와 요청을 구축

+0

나는 웹 응용 프로그램 CMS를 개발하고 있습니다. 내 응용 프로그램의 일부를 ascx 파일로 호출해야합니다. 나는 페이지를 다시 게시하고 싶지 않아. 그렇게 할 수있는 더 좋은 방법이 있습니까? 나는 모든 부분을 너무로드하고 싶지 않다. – Raika

+0

@Raika - 올바른 방법으로 작업하고 싶다면 ASP.NET WebForms AJAX (UpdatePanel 등)를 지원하고 컨트롤을 호환 가능하게 만듭니다. 그러면 AJAX를 통해 동적으로로드 할 수 있습니다. 덕분에 –

+0

. 웹 파트에 대한 링크를 더 많이 주거나 웹 파트에 대한 자습서를 완성 할 수 있습니까? – Raika

3
public ActionResult Foo() 
{ 
    return new ContentResult 
    { 
     Content = RenderPartialToString("Foo.ascx", null), 
     ContentType = "text/html" 
    }; 
} 

//http://www.klopfenstein.net/lorenz.aspx/render-partial-view-to-string-asp-net-mvc-benchmark 
public static string RenderPartialToString(string controlName, ViewDataDictionary viewData) 
{ 
    ViewPage vp = new ViewPage(); 

    vp.ViewData = viewData; 

    Control control = vp.LoadControl(controlName); 
    vp.Controls.Add(control); 

    StringBuilder sb = new StringBuilder(); 

    using (StringWriter sw = new StringWriter(sb)) 
    { 
     using (HtmlTextWriter tw = new HtmlTextWriter(sw)) 
     { 
      vp.RenderControl(tw); 
     } 
    } 

    return sb.ToString(); 
} 
+0

감사하지만 webforms mvc 아닙니다 사용하고 있습니다 !!! : – Raika

+0

이것은 멋진 트릭입니다 – Vadim

+0

이것은 ASP.NET MVC입니다. OP는 WebForms를 사용하고 있습니다. –

관련 문제