views:

61

answers:

2

Hi all,

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.

+3  A: 

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

Claudio Redi
Thanks for the answer... Sorry I was vague, pretty much I just wanted to get the rendered HTML from the user control. I should have specified that I wanted to use jQuery AJAX to get the control's html.
Brian D.
What about the page life cycle? you're only calling render here. Suppose there was startup logic in page_load or one of the other events?
dfowler
yes I know, but if he really need to do this, then he would need to adpapt it. Trade off decision.
Claudio Redi
+1  A: 

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: http://geekswithblogs.net/rashid/archive/2007/08/11/Loading-UserControl-Dynamically-in-UpdatePanel.aspx

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);
Maxim Gershkovich
Can't believe I didn't think of just putting the control on a page and calling the page. Kind of sucks that you can't just render the control by its self... now I'll have to make a bunch of .aspx pages that only have the control's tag on it...
Brian D.
I've been there done that. Took me a while to realize the first time also. The only problem with this approach is you will need to filter out the html tags on the client side unless you put it into an iframe.Also you may run into some issues with how events are handled.
Maxim Gershkovich