ArgisIsland
ArgisIsland

Reputation: 407

Hide dropdown box when ASP.NET dropdownlist is empty

since I combined an asp:dropdownlist with an asp:checkboxlist, I have the problem in IE(8) and Firefox (Chrome works fine) that everytime I click the DropDownList, the empty box appears in addition to the popup I manually open when the dropdownlist is clicked.

My question now is: How can I hide this empty box (since there are no entries in it), but keep the dropdown element? I don't want to replace this component, since it still should look like a dropdownlist. If I change it to a text box it's not clear anymore that it can be used as a dropdown.

This is what I currently have in place:

<div style="width: 190px;" class="right">
    <!-- the original drop down list -->
    <asp:DropDownList CssClass="dropdownbox" ID="ddlCountry" runat="server">
    </asp:DropDownList>
    <cc1:PopupControlExtender ID="ddlCountry_PopupControlExtender" runat="server" DynamicServicePath=""
        Enabled="True" ExtenderControlID="" TargetControlID="ddlCountry" PopupControlID="pnlPopup"
        OffsetY="20">
    </cc1:PopupControlExtender>
    <!-- Popup control extender that maps the country list to the dropdown list -->
    <asp:Panel ID="pnlPopup" runat="server" CssClass="dropdowncheckbox">
        <!-- List of countries with a checkbox for each entry -->
        <asp:CheckBoxList ID="countryList" runat="server" 
            DataTextField="Countries" DataValueField="Countries" AutoPostBack="True"
            OnSelectedIndexChanged="countryList_SelectedIndexChanged">
        </asp:CheckBoxList>
    </asp:Panel>
</div>

If there is a component that fits my purpose better, please let me know. Thanks a lot in advance for your suggestions.

Upvotes: 2

Views: 8598

Answers (4)

Juan
Juan

Reputation: 1382

Encapsulate the dropdown in a div, give the div an id and the runat="server" property

Check in the code behind of the data to load the dropdown is empty set the div to visible false; if not, set the div to visible true.

Upvotes: 0

Helmer
Helmer

Reputation: 126

do something like this in c#:

if(dropdownlist.Items.Count == 0)
{
      dropdownlist.Visible = false;
}
else
{
      dropdownlist.Visible = true;
}

Upvotes: 0

Max
Max

Reputation: 11

Place this code in the event you need to trigger the change This will work for c#

if(dropdownlist.Items.Count == 0)
{
dropdownlist.Attributes["style"] = "display: none;";
}
else
{
dropdownlist.Attributes["style"] = "";
}

Upvotes: 1

IrishChieftain
IrishChieftain

Reputation: 15262

Simply set the Drop Down List Enabled property to false and toggle it to true when it has entries.

dropdownbox.Enabled = false;

Upvotes: 0

Related Questions