Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable first item in DropDownList

I'm trying to disable the first item in a drop-down list, which is linked to a "submit" button. On first loading the page, I want the list to display to the first item in the list, --Select-- without being able to select it.

Following the responses to this question, I'm using:

dd.Items[0].Attributes.Add("disabled", "disabled");

where dd refers to the DropdownList..

This works well but only if I activate it after the submit button is clicked. This means that when the page first loads, I can still select the first item.

If I put the above code in the Page_Load method, the list defaults to the next item and --Select-- is no longer displayed.

Does anyone know a way to have the list continue to display the first item but disable selection of it?

like image 798
Robert Avatar asked Dec 19 '25 23:12

Robert


2 Answers

I think you want to do this:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="--Select One--" Value="" />   
</asp:DropDownList>
like image 55
Steven Mays Avatar answered Dec 22 '25 13:12

Steven Mays


Per Robert's request, I copied my comment as the answer:

"You could either disable the submit button if the index of your dropdown is 0 (--Select--), or you could check if the index is 0 when the user hits your submit button and just return (or do nothing)."

like image 22
B L Avatar answered Dec 22 '25 11:12

B L