【问题标题】:Add a single control to multiple placeholders将单个控件添加到多个占位符
【发布时间】:2014-08-10 15:46:04
【问题描述】:

我的 page.aspx 中有两个占位符:

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

// Other tags

<asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>

我在 page.aspx.cs 中创建了一个 HtmlGenericControl,并希望将它添加到两个 PlaceHolders 中:

HtmlGenericControl NewControl = new HtmlGenericControl("div");
NewControl.ID = "newDIV";
NewControl.Attributes.Add("class", "myClass");
NewControl.InnerHtml = "**myContent**";
PlaceHolder1.Controls.Add(NewControl);
PlaceHolder2.Controls.Add(NewControl);

问题是只有最后一个添加才生效!

线

PlaceHolder1.Controls.Add(NewControl);

不起作用!

我错了吗?

提前致谢。

【问题讨论】:

  • 一个控件不能是多个父控件的子控件。您必须创建 HtmlGenericControl 两次。

标签: asp.net-placeholder


【解决方案1】:

一个控件不能是多个父控件的子控件。您必须创建您的 HtmlGenericControl 两次:

Func<HtmlGenericControl> createControl = () => {
    HtmlGenericControl newControl = new HtmlGenericControl("div");
    newControl.ID = "newDIV";
    newControl.Attributes.Add("class", "myClass");
    newControl.InnerHtml = "**myContent**";
    return newControl;
};

PlaceHolder1.Controls.Add( createControl() );
PlaceHolder2.Controls.Add( createControl() );

【讨论】:

    猜你喜欢
    • 2011-01-05
    • 2013-06-24
    • 1970-01-01
    • 2010-11-09
    • 2014-04-08
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    相关资源
    最近更新 更多