Label control child controls

Discussion in 'ASP .Net Web Controls' started by SteveM, Feb 13, 2009.

  1. SteveM

    SteveM Guest

    I recently came accross what I consider strange behavior with the asp:Label
    control . I created a simple page that illustrates the issue.

    <%@ Page Language="C#" AutoEventWireup="true" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    <script runat="server">

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    Label1.Enabled = ((CheckBox)sender).Checked;

    <html xmlns="" >
    <head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    <asp:CheckBox ID="CheckBox1" runat="server" Checked="true"
    AutoPostBack="true" Text="Toggle Enabled"
    OnCheckedChanged="CheckBox1_CheckedChanged" />
    <asp:Label ID="Label1" runat="server">
    <br />Test <asp:TextBox ID="TextBox1"

    When you run this page, you will see the checkbox and the label control
    contents (text and edit box). If you click the checkbox, the label control
    contents will be displayed as disabled as you woud expect. The oddity
    happens when you click the checkbox again. In this case the label doesn't
    display at all. On closer inspection, what is happening is that somewhere
    after the Page Init event and before the Page Load event the Label control is
    removing it's two child controls. The child controls still exist (e.g.,
    TextBox1 exists) but their parent properties are null.

    Interestingly, I noticed that if the Label control only has one child
    control the issue does not occur. That is, the single child control remains
    a child of the Label control even after the second click of the checkbox.
    This can be demonstrated by wrapping the contents of the label control in a
    placeholder control as shown here.

    <asp:Label ID="Label1" runat="server">
    <asp:placeHolder ID="PlaceHolder1" runat="server">
    <br />Test <asp:TextBox ID="TextBox1"

    With this scenario, the contents of the Label control is disabled and
    enabled as you would expect.

    I am not asking for tips on how to do the same thing in different ways (I've
    already done that). I am curious as to why things happen the way they do
    given this scenario. The child controls exist during the Init event so my
    guess is that something in the ViewState is causing the Label control to
    orphan it's child controls (only if more than one exists), but what?

    Thanks for any insight in advance.
    - Steve
    SteveM, Feb 13, 2009
    1. Advertisements

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. Jeff Rodriguez
    David Schwartz
    Dec 9, 2003
  2. Chad Scharf
    Chad Scharf
    Oct 18, 2007
  3. Replies:
  4. Replies:
    Teemu Keiski
    Dec 8, 2005
  5. Knut Krueger
    Knut Krueger
    Aug 20, 2013