Add same control to different controls

Discussion in 'ASP .Net' started by Andrew Jocelyn, Jun 6, 2008.

  1. Hi

    Maybe it's because it's Friday afternoon but I can't work out how to simply
    use the same Label control in two different PlaceHolder controls. This is
    what happens. When I add the Label to the second PlaceHolder it's removed
    from the first. I want any changes to the Label to be reflected in both
    PlaceHolders.

    Label lb = new Label();
    lb.Text = "Label";
    PlaceHolder1.Controls.Add(lb);
    Response.Write(PlaceHolder1.HasControls().ToString()); // true
    PlaceHolder2.Controls.Add(lb);
    Response.Write(PlaceHolder1.HasControls().ToString()); // false


    In real live the Label with be a custom control with child controls. What
    should I do?

    Thanks
    Andrew
     
    Andrew Jocelyn, Jun 6, 2008
    #1
    1. Advertising

  2. Andrew Jocelyn

    Riki Guest

    This is how ASP.NET controls behave.
    You can't add them twice.

    You'll need to write some code that updates both labels, and do all updates
    through that code.

    --
    Riki

    "Andrew Jocelyn" <> wrote in message
    news:uf1wvr$...
    >
    > Hi
    >
    > Maybe it's because it's Friday afternoon but I can't work out how to
    > simply use the same Label control in two different PlaceHolder controls.
    > This is what happens. When I add the Label to the second PlaceHolder it's
    > removed from the first. I want any changes to the Label to be reflected in
    > both PlaceHolders.
    >
    > Label lb = new Label();
    > lb.Text = "Label";
    > PlaceHolder1.Controls.Add(lb);
    > Response.Write(PlaceHolder1.HasControls().ToString()); // true
    > PlaceHolder2.Controls.Add(lb);
    > Response.Write(PlaceHolder1.HasControls().ToString()); // false
    >
    >
    > In real live the Label with be a custom control with child controls. What
    > should I do?
    >
    > Thanks
    > Andrew
    >
     
    Riki, Jun 6, 2008
    #2
    1. Advertising

  3. create a second label control and add it.
    --
    Share The Knowledge. I need all the help I can get and so do you!


    "Andrew Jocelyn" wrote:

    >
    > Hi
    >
    > Maybe it's because it's Friday afternoon but I can't work out how to simply
    > use the same Label control in two different PlaceHolder controls. This is
    > what happens. When I add the Label to the second PlaceHolder it's removed
    > from the first. I want any changes to the Label to be reflected in both
    > PlaceHolders.
    >
    > Label lb = new Label();
    > lb.Text = "Label";
    > PlaceHolder1.Controls.Add(lb);
    > Response.Write(PlaceHolder1.HasControls().ToString()); // true
    > PlaceHolder2.Controls.Add(lb);
    > Response.Write(PlaceHolder1.HasControls().ToString()); // false
    >
    >
    > In real live the Label with be a custom control with child controls. What
    > should I do?
    >
    > Thanks
    > Andrew
    >
    >
    >
     
    Yankee Imperialist Dog, Jun 6, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad Scharf
    Replies:
    3
    Views:
    925
    Chad Scharf
    Oct 18, 2007
  2. Christopher Brewster
    Replies:
    5
    Views:
    358
    John Machin
    Nov 14, 2008
  3. bluebaron
    Replies:
    3
    Views:
    768
    Jonathan N. Little
    Nov 4, 2009
  4. Balaji
    Replies:
    0
    Views:
    472
    Balaji
    Jul 7, 2003
  5. Guest
    Replies:
    2
    Views:
    187
    Foo Man Chew
    Dec 29, 2003
Loading...

Share This Page