Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - Load user control using AJAX?

I'm not sure if what I'm trying to do is possible - pretty much I just want to call a user control using AJAX and get the rendered html of the control. However, when I try and fetch the control I get the following error message:

This type of page is not served.

Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.ascx' may be incorrect. Please review the URL below and make sure that it is spelled correctly.

Requested URL: /Controls/ClientFormControl.ascx

Is it possible to make this type of page servable, or is there a specific way you need to call it? I know such things are easy in MVC frameworks...

Thanks in advance.

like image 396
Brian DiCasa Avatar asked Dec 22 '22 02:12

Brian DiCasa


1 Answers

Not sure how you're doing it since you provided no code. Also not sure why you would need such a weird thing :-) Not very common, probably there is an alternative approach. You may provide more details so we can suggest a better option if any.

I'd say you could do that but you need to create the control and call the render programatically like this

TextWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
userControl.RenderControl(htmlWriter);
string html = stringWriter.ToString();

After this, you have to append the html variable to your response.

This code should be placed on a method marked as WebMethod or an HTTP Handler so you can call it from your javacript.

Sample: Calling WebMethods from javascript

Sample: Calling HTTP Handlers from javascript

like image 90
Claudio Redi Avatar answered Dec 29 '22 11:12

Claudio Redi