haln
haln

Reputation: 50

asp:label change visibility after hiding it

I've got an asp:Label and a asp:DropDownList that I want to be able to switch back and forth between visible and invisible when clicking on some buttons. Right now, my code looks like

aspx file

<asp:Label AssociatedControlID="statusFilter" id="statusFilterLabel" runat="server" CssClass="filterLabel">Status
    <asp:DropDownList ID="statusFilter" runat="server" CssClass="filterInput" AutoPostBack="true" OnSelectedIndexChanged="anyFilter_SelectedIndexChanged" AppendDataBoundItems="True">
        <asp:ListItem Selected="True" Value=" 0">&lt;All&gt;</asp:ListItem>
    </asp:DropDownList>
</asp:Label>
<asp:Button Text="ALL" ID="AllTabButton" CssClass="tabButton" runat="server" OnClick="AllTab_Click" />
<asp:Button Text="Arrived" ID="ArrivedTabButton" CssClass="tabButton" runat="server" OnClick="ArrivedTab_Click" />

code behind

    protected void AllTab_Click(object sender, EventArgs e)
    {
        AllTabButton.CssClass = "tabButtonClicked";
        ArrivedTabButton.CssClass = "tabButton";
        statusFilter.Visible = true;
        statusFilterLabel.Visible = true;
    }
    protected void ArrivedTab_Click(object sender, EventArgs e)
    {
        AllTabButton.CssClass = "tabButton";
        ArrivedTabButton.CssClass = "tabButtonClicked";
        statusFilter.Visible = false;
        statusFilterLabel.Visible = false;
    }

The only problem is that if I try to set Visible=true after setting Visible=false it would give me an error Unable to find control with id 'statusFilter' that is associated with the Label 'statusFilterLabel'.

I tried doing some other things instead of using Visible, like setting the style: statusFilter.Style.Add("display", "block") and setting the cssclass: statusFilter.CssClass = "displayBlock"but the resulting error always showed up.

An asp:Panel would work, but I'm avoiding using that because I want my asp:Label and asp:DropDownList to line up with several other labels and dropdownlists; putting in a panel would make them not line up properly.

I'm guessing there is something I'm missing, something I just don't get, but I can't seem to figure out what that is. If anybody has any clue as to what's happening, I would really appreciate the help!

Upvotes: 2

Views: 1718

Answers (1)

MisterZimbu
MisterZimbu

Reputation: 2713

It's not able to always find the control on postback because it's a child of statusFilter. Move the input field outside of the label:

<asp:Label AssociatedControlID="statusFilter" id="statusFilterLabel" runat="server" CssClass="filterLabel">Status
</asp:Label>
<asp:DropDownList ID="statusFilter" runat="server" CssClass="filterInput" AutoPostBack="true" OnSelectedIndexChanged="anyFilter_SelectedIndexChanged" AppendDataBoundItems="True">
    <asp:ListItem Selected="True" Value=" 0">&lt;All&gt;</asp:ListItem>
</asp:DropDownList>

Upvotes: 1

Related Questions