Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

List control child controls

 
 
SteveM
Guest
Posts: n/a
 
      02-13-2009
I came across some odd behavior with the Label control recently. The
following simple page demonstrates the behavior:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Label1.Enabled = ((CheckBox)sender).Checked;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="true"
AutoPostBack="true" Text="Toggle Enabled"
OnCheckedChanged="CheckBox1_CheckedChanged" />
<asp:Label ID="Label1" runat="server">
<br />Label <asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
</asp:Label>
</div>
</form>
</body>
</html>

If you load this page and click the checkbox the label control will display
it's contents as disabled as you would expect. When, however, you click the
checkbox a second time the label control doesn't display anything. Upon
closer inspection, you will notice that after the second click the label
control loses it's child controls some time after the Page Init event and
before the Page Load event. The child controls still exist (e.g, TextBox1
still exists) but their parent properties are null.

Interestingly, this issue doesn't occur if the label control only has a
single child control. Modifying the page by wrapping the label control
contents in a single placeholder control changes the behavior to what you
would expect.

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

Running with this change causes the label contents to toggle between enabled
and disabled as you would expect.

I am not looking for suggestions about other ways to accomplish the same
behavior. I've already done that. I am just curious as to why the label
control feels the need to orphan it's child controls in this scenario. Any
insight would be greatly appreciated.

Thanks!
 
Reply With Quote
 
 
 
Reply

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. sinelnikov.andrei@gmail.com ASP .Net Building Controls 1 12-08-2005 06:47 AM
Control.Controls bug? Control's child controls missing at the run time. sinelnikov.andrei@gmail.com ASP .Net Web Controls 1 12-08-2005 06:46 AM
HELP! Child controls of child controls not visible to web app lisa@starways.net 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



Advertisments