ASP.NET - 加载用户控件使用AJAX?控件、加载、用户、ASP

2023-09-10 19:23:51 作者:-你是我的情人

我不知道,如果我想要做的是可能的 - pretty的多,我只是想打电话给使用AJAX用户控件,并得到控制的呈现的HTML。然而,当我试图获取我收到以下错误信息控制:

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.

说明:您请求的页面的类型不提供服务,因为它已被明确禁止。扩展的.ascx'可能不正确。请检查下面的URL并确保其拼写正确。

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.

请求的URL:/Controls/ClientFormControl.ascx

Requested URL: /Controls/ClientFormControl.ascx

是否有可能使这种类型的页面servable的,或者是有需要调用它以特定的方式?我知道这样的事情是很容易的MVC框架......

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

在此先感谢。

推荐答案

你是不是看问题正确。一个用户控件无法呈现,除非它被包含在一个WebForm。

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.

另外,您可以使用一个UpdatePanel,然后将用户控件添加到当前页面编程方式在服务器端(在类似的updatepanel_load事件)。

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)

要在这里跟进的是一个很好的例子:http://geekswithblogs.net/rashid/archive/2007/08/11/Loading-UserControl-Dynamically-in-UpdatePanel.aspx

To follow up here is a great example: http://geekswithblogs.net/rashid/archive/2007/08/11/Loading-UserControl-Dynamically-in-UpdatePanel.aspx

看起来像把戏是一个占位符和函数我不知道LoadControl的()。 System.Web.UI.TemplateControl.LoadControl

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