ASP.NET - load user control using AJAX?

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

This page type is not served.

Description. The type of page requested is not served because it is explicitly prohibited. The .ascx extension may be incorrect. Check out the URL below to make sure it is spelled correctly.

Requested URL: /Controls/ClientFormControl.ascx

Is it possible to make this type of page available, or is there a specific way to call it? I know that in MVC infrastructures this is easy ...

Thanks in advance.

+4
source share
2 answers

You are not looking at the problem correctly. User control cannot be displayed if it is not contained in WebForm.

The correct solution to this problem is to create a page with only usercontrol contained in it, and then render / paste it as necessary.

Alternatively, you can use the update panel, and then add user control to the current page programmatically on the server side (something like the updatepanel_load event).

(actually it seems like my second solution is not working - checking it now)

As an example, you can give a great example: http://geekswithblogs.net/rashid/archive/2007/08/11/Loading-UserControl-Dynamically-in-UpdatePanel.aspx

It seems like the trick is placeholder and function that I did not know about LoadControl (). System.Web.UI.TemplateControl.LoadControl

PlaceHolder1.Controls.Clear(); UserControl uc = (UserControl)LoadControl(controlPath); PlaceHolder1.Controls.Add(uc); 
+3
source

Not sure how you do this, since you haven't provided the code. Also not sure why you will need such a strange thing :-) Not very common, maybe there is an alternative approach. You can provide more detailed information so that we can offer the best option, if any.

I would say that you could do this, but you need to create a control and invoke rendering programmatically, like this

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

After that, you should add the html variable to your answer.

This code must be placed in a method marked as WebMethod or an HTTP handler so that you can call it from your javacript.

Example: calling WebMethods from javascript

Example: calling HTTP handlers from javascript

+8
source

Source: https://habr.com/ru/post/1313214/


All Articles