سؤال

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?

هل كانت مفيدة؟

المحلول 2

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

نصائح أخرى

I think you want to do this:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="--Select One--" Value="" />   
</asp:DropDownList>
DropDownList1.Items[0].Enabled = false;

or

DropDownList1.Items[0].Attributes["style"] = "display:none";
DropDownList1.Items[0].Attributes["disabled"] = "disabled";
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top