问题描述
我正在尝试禁用下拉列表中的第一项,该列表链接到提交按钮。
第一次加载页面时,我希望列表显示到列表中的第一项-选择-
,而无法选择它。
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");
其中 dd
是指DropdownList。
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.
如果将上面的代码放入 Page_Load
方法,该列表默认为下一项,并且-Select-
不再显示。
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?
推荐答案
Per Robert's请求,我将评论复制为答案:
Per Robert's request, I copied my comment as the answer:
如果下拉列表的索引为0(--Select--),则可以禁用提交按钮,或者您可以在用户点击您的提交按钮并返回(或不执行任何操作)时检查索引是否为0。
"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)."
这篇关于禁用DropDownList中的第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!