Search code examples
asp.netajaxasp.net-ajaxuser-controls

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.


Solution

  • You are not looking at the problem correctly. A usercontrol cannot be rendered unless it is contained in a WebForm.

    The correct solution to this problem is to create a page with only the usercontrol contained on it and then render/inject it as required.

    Alternatively you can use an updatepanel and then add the usercontrol to the current page programmatically on the server side (in something like the updatepanel_load event).

    (actually it looks like my second solution doesnt work - checking it out now)

    To follow up here is a great example: Link

    Looks like the trick is a placeholder and a function i was unaware of LoadControl(). System.Web.UI.TemplateControl.LoadControl

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