ASP.NET:如何动态地在同一个aspx页面中添加两个Web用户控件?

时间:2020-12-09 16:46:33

How can I add two Web User Controls in the same aspx page dynamically? When I try to do that I get the following error

如何动态地在同一个aspx页面中添加两个Web用户控件?当我尝试这样做时,我收到以下错误

Unable to cast object of type 'ASP.webusercontroladult_ascx' to type 'WebUserControl'.

无法将“ASP.webusercontroladult_ascx”类型的对象强制转换为“WebUserControl”类型。

I am able to add 1 Web User control dynamically as

我可以动态添加1个Web用户控件

for (int i = 0; i < 2; i++)
{
    Control uc = (WebUserControl)Page.LoadControl("WebUserControlAdult.ascx"); 
    Panel1.Controls.Add(uc);
    Panel1.Controls.Add(new LiteralControl(@"<br />"));
}

4 个解决方案

#1


Have you tried setting an ID for the user control? ie

您是否尝试为用户控件设置ID?即

 Control uc = (WebUserControl)Page.LoadControl("WebUserControlAdult.ascx");
 uc.id = "Dyn" + i.tostring();
 Panel1.Controls.Add(uc);

Or is the UserControl being cast to the wrong type? Maybe

或者UserControl被转换为错误的类型?也许

(WebUserControlAdult)Page.LoadControl ...

#2


The problem appears to be in the cast rather than the loop.

问题似乎是在演员表而不是循环中。

I don't see the requirement for the explicit cast in your case. You could simply do something like :

在你的案例中,我没有看到明确演员的要求。你可以简单地做一些事情:

for (int i = 0; i < 2; i++)
{
  Control uc = Page.LoadControl("WebUserControlAdult.ascx"); 
  Panel1.Controls.Add(uc);
  Panel1.Controls.Add(new LiteralControl(@"<br />"));
}

And that should work. However, if you needed to set some explicit properties exposed by the WebUserControlAdult class, then you would need to cast. In that case, you should cast to the usercontrol class type (as @benophobia has illustrated).

那应该有用。但是,如果需要设置WebUserControlAdult类公开的某些显式属性,则需要进行强制转换。在这种情况下,您应该转换为usercontrol类类型(如@benophobia所示)。

#3


If your WebUserControl is a databound control, you may want to consider using Repeater Controls.

如果您的WebUserControl是数据绑定控件,您可能需要考虑使用Repeater控件。

Using a repeater control, you would only need to put your control into the ItemTemplate of the repeater, e.g.,

使用转发器控件,您只需要将控件放入转发器的ItemTemplate中,例如,

<asp:Repeater ID="bunchOfControls" runat="server">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <namespace:WebUserControlAdult runat="server />
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

#4


dealing with dynamic user controls can be a pain in the ass.

处理动态用户控件可能是一个痛苦的屁股。

as a rule of thumb i follow, whenever you create a dynamic user control, then you must set it's ID so ASP.net can reallocate it on post back, and to keep the controls values after post back you should reload your user controls on Page_Init event.

根据我的经验,每当你创建一个动态用户控件时,你必须设置它的ID,以便ASP.net可以在回发后重新分配它,并在发回后保留控件值,你应该重新加载Page_Init上的用户控件事件。

hope this helps.

希望这可以帮助。

#1


Have you tried setting an ID for the user control? ie

您是否尝试为用户控件设置ID?即

 Control uc = (WebUserControl)Page.LoadControl("WebUserControlAdult.ascx");
 uc.id = "Dyn" + i.tostring();
 Panel1.Controls.Add(uc);

Or is the UserControl being cast to the wrong type? Maybe

或者UserControl被转换为错误的类型?也许

(WebUserControlAdult)Page.LoadControl ...

#2


The problem appears to be in the cast rather than the loop.

问题似乎是在演员表而不是循环中。

I don't see the requirement for the explicit cast in your case. You could simply do something like :

在你的案例中,我没有看到明确演员的要求。你可以简单地做一些事情:

for (int i = 0; i < 2; i++)
{
  Control uc = Page.LoadControl("WebUserControlAdult.ascx"); 
  Panel1.Controls.Add(uc);
  Panel1.Controls.Add(new LiteralControl(@"<br />"));
}

And that should work. However, if you needed to set some explicit properties exposed by the WebUserControlAdult class, then you would need to cast. In that case, you should cast to the usercontrol class type (as @benophobia has illustrated).

那应该有用。但是,如果需要设置WebUserControlAdult类公开的某些显式属性,则需要进行强制转换。在这种情况下,您应该转换为usercontrol类类型(如@benophobia所示)。

#3


If your WebUserControl is a databound control, you may want to consider using Repeater Controls.

如果您的WebUserControl是数据绑定控件,您可能需要考虑使用Repeater控件。

Using a repeater control, you would only need to put your control into the ItemTemplate of the repeater, e.g.,

使用转发器控件,您只需要将控件放入转发器的ItemTemplate中,例如,

<asp:Repeater ID="bunchOfControls" runat="server">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <namespace:WebUserControlAdult runat="server />
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

#4


dealing with dynamic user controls can be a pain in the ass.

处理动态用户控件可能是一个痛苦的屁股。

as a rule of thumb i follow, whenever you create a dynamic user control, then you must set it's ID so ASP.net can reallocate it on post back, and to keep the controls values after post back you should reload your user controls on Page_Init event.

根据我的经验,每当你创建一个动态用户控件时,你必须设置它的ID,以便ASP.net可以在回发后重新分配它,并在发回后保留控件值,你应该重新加载Page_Init上的用户控件事件。

hope this helps.

希望这可以帮助。