简体   繁体   中英

How to make an asp dropdown list item unselectable

I have written following code to display drop down list :

<asp:DropDownList AutoPostBack="True" ID="ddlCities" runat="server" class="form-control input-sm" placeholder="" TabIndex="5">
       <asp:ListItem>Select City</asp:ListItem>
       <asp:ListItem value="3">Ahmedabad (All) ---------------</asp:ListItem>
       <asp:ListItem value="3_3004">&nbsp;&nbsp;&nbsp;&nbsp;Ahmedabad East</asp:ListItem>
       <asp:ListItem value="3_3005">&nbsp;&nbsp;&nbsp;&nbsp;Ahmedabad West</asp:ListItem>
       <asp:ListItem value="3_3006">&nbsp;&nbsp;&nbsp;&nbsp;Ahmedabad-Bopal and Surroundings</asp:ListItem>
       <asp:ListItem value="3_3007">&nbsp;&nbsp;&nbsp;&nbsp;Ahmedabad-Gandhinagar</asp:ListItem>
       <asp:ListItem value="3_3008">&nbsp;&nbsp;&nbsp;&nbsp;Ahmedabad-Sabarmati and Surroundings</asp:ListItem>
       <asp:ListItem value="3_3009">&nbsp;&nbsp;&nbsp;&nbsp;Ahmedabad-SG Highway and Surroundings</asp:ListItem>
</asp:DropDownList>

I want to make following item inside dropdown unselectable :

<asp:ListItem value="3">Ahmedabad (All) ---------------</asp:ListItem>

Note : I CAN'T USE OPTGROUP!!!!

and i don't want to hide it. it will be shown in dropdown, but user can't select this item.

I have tried adding 'disabled' attribute, but it hides that item.

i have also tried :

ddlCities.Items[1].Attributes.Add("Style", "cursor:not-allowed");

It doesn't allow cursor, but still user can select this item, is there any other way to make this particular item unselectable??

Even though you said that disabled attribute hides the element, you are wrong. Disabled attribute is exactly what you should use for this:

    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Value="1">First</asp:ListItem>
        <asp:ListItem Value="2" disabled="disabled">Second</asp:ListItem>
        <asp:ListItem Value="3">First</asp:ListItem>
    </asp:DropDownList>

Result:

在此处输入图片说明

I think you're looking for something like this . Please see if it can help you.

[Update] just looked back to your post and understand you cannot use OPTGROUP. May i know the reason? as your requirement needs this option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM