Search code examples
c#asp.netdrop-down-menuvisual-web-developer-2010

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?


Solution

  • 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)."