Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Label control child controls

Thread Tools

Label control child controls

Posts: n/a
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">
<asplaceHolder 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
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
this.Controls.Add(Control) Does Not Add Control to child collection (this.Controls.Count == 0) Chad Scharf ASP .Net 3 10-18-2007 03:53 PM
Control.Controls bug? Control's child controls missing at the run time. ASP .Net Building Controls 1 12-08-2005 06:47 AM
Control.Controls bug? Control's child controls missing at the run time. ASP .Net Web Controls 1 12-08-2005 06:46 AM
HELP! Child controls of child controls not visible to web app ASP .Net Building Controls 0 05-05-2005 09:14 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM