-1

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??

2
  • Possible duplicate of make drop down list item unselectable Commented Jul 7, 2017 at 5:54
  • @AzarShaikh the solution said in the link won't work, it will hide the item Commented Jul 7, 2017 at 5:58

2 Answers 2

4

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:

enter image description here

Sign up to request clarification or add additional context in comments.

12 Comments

i have already mentioned that disabled attribute will hide that field. what you have shown as RESULT, is what exactly i want, but <asp:ListItem Value="2" disabled="disabled">Second</asp:ListItem> will hide that item
@ace I just tested that and it does not hide it. Do you have some javascript or maybe css that hides it? Asp.net does not hide it, it will just disable it.
i don't have any javascript or css that hides it, but i have ddlCities.Items[1].Attributes.Add("Style", "cursor:not-allowed"); this in c# code behind file
can you please share the code snippet which works for you?
Moreover you can't use disabled="disabled", you can use enabled="true"
|
-1

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.

2 Comments

Please do not post link-only answers. If the link dies, your answer becomes useless.
@NareshPodishetty i have already tried the solution in the said link, it doesn't works at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.