0

There are two placeholders in my page.aspx:

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

// Other tags

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

I've created one HtmlGenericControl in page.aspx.cs and want to add it in both 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);

The problem is that just the last Add takes effect !

The Line

PlaceHolder1.Controls.Add(NewControl);

does not work !

Am I wrong ?

Thanks in advance.

Muhamad Jafarnejad
  • 2,521
  • 4
  • 21
  • 34
  • 1
    A control cannot be a child of more than 1 parent control. You must create your HtmlGenericControl twice. – Dai Aug 10 '14 at 07:44

1 Answers1

1

A control cannot be a child of more than 1 parent control. You must create your HtmlGenericControl twice:

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() );
Dai
  • 141,631
  • 28
  • 261
  • 374