Disable first item in DropDownList

回眸只為那壹抹淺笑 提交于 2019-12-08 06:56:32

问题


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?


回答1:


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




回答2:


I think you want to do this:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="--Select One--" Value="" />   
</asp:DropDownList>


来源:https://stackoverflow.com/questions/14198870/disable-first-item-in-dropdownlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!