S
SteveM
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!
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!