Download ascx via jQuery

Is there a way to upload an ascx file using jQuery?

UPDATE

thanks @Emmett and @Yads. I use a handler with the following jQuery ajax code:

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'); } }); 

but I get an error. Is my code wrong?

Other Update : I am using

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

and this is what I get:

Invalid JSON:
Test => (this test is a label inside my ascx)

and my ascx file after the error !!!

Other Update :

my ascx file looks something like this:

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

but when ajax is called, I get this error in asp :: (

The ctl00_ddl control of the DropDownList type must be placed inside the form tag using runat = server.

thanks @Yads. but his solution only works with the html tag.

+6
jquery webforms
source share
3 answers

Creating an Emmett Solution

 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; } } } 

Use the following jquery query

 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'); } }); 
+10
source share
 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(); } 
+3
source share

*. ascx files are displayed on the server side (inside the * .aspx page), and not on the client side (where JavaScript is running).

One option would be to create an empty * .aspx, place the user control on the * .aspx page, and then get that page through jQuery and upload the result to the page.

Edit

Based on your comment, I have another suggestion:

If you are developing a CMS-style application, you must create your * .ascx controls so that they are compatible with the ASP.NET AJAX Toolkit . This will allow users to add content to the page without a full refresh.

If you really want to do something nice for the user, you should check out the web parts and ASP.NET AJAX , since the web parts were really there so that users can customize the content on their pages.

+1
source share

All Articles