views:

26

answers:

3

I want to show and hide a label and its control. I can do this in c# in the code behind. But, I can only show/hide the control. Any ideas?

<asp:label AssociatedControlID="thisLabel" runat="server">This:
     <asp:label ID="thisLabel" CssClass="ascontrol" runat="server" />
</asp:label>

I want to be able to show and hide that whole thing depending on what user gets to the page. I just need to know how to show/ hide that whole thing in the c# code behind...cannot seem to get the visibility of the wrapper label to go away.

A: 

You haven't supplied a server-side Id:

<asp:Label ID="label_MyControl" AssociatedControlID="txt_MyControl" runat="server" />
<asp:TextBox ID="txt_MyControl" runat="server" />

What you've done is nest a asp:Label control within another asp:Label control....

Matthew Abbott
+1  A: 

Since I normally hide more than one field contiguously, I tend to wrap the whole thing in an asp:Panel and hide the panel. However, that's just my particular usage. But since it's my usage, I tend to block those sorts of things out into panels even for something as simple as your example.

Just my nickel's worth, your mileage may vary, as always.

drachenstern
A: 

It should work if you get you r markup correct, like this:

<asp:Label ID="lblYear" runat="server" Text="Year (yyyy):" 
    AssociatedControlID="txtYear"></asp:Label>
<asp:TextBox ID="txtYear" runat="server" Columns="30" MaxLength="4"></asp:TextBox>

Then in the code behind you could have:

lblYear.visible = False
txtYear.Visible = False

Now, my understanding of the "AssociatedControlID" property of an asp:label is mainly for accessibility purposes. You don't need to have the AssociatedControlID value set to make things work as I've shown.

Ken Ray
That is correct. The associated control is actually HTML standard and it allows for things like clicking a label on the form to put the focus to the element assigned to it. Windows forms designers have had that option for a number of years as well, so that if you click (or ALT+<letter>) a label, it puts the focus on the field. Just basics of form manipulation. Also, good for accessibility, but primarily because textual forms are textual.
drachenstern