如何使下拉列表框初始化时不让第一个处于选中状态?

时间:2021-08-01 21:21:54
如何使下拉列表框初始化时不让第一个处于选中状态?
    我的ASP.NET页面要求改变下拉列表框的选项时触发服务器事件,如果第一个被选中了,那么我在选择第一项时就没有办法触发事件,可我又不想用Insert()添加一个空列表项的办法,因为这会使列表项多出一行。
    我只希望能在下拉列表框初始化时,显示的是一空行,不是第一项,我该怎么办呢?

8 个解决方案

#1


将其DragDown属性设置一下,有好多选项。

#2


in Page_Load after possible initialization, try to do

if (!IsPostBack)
   List1.SelectedIndex = -1;

#3


sorry, I was wrong, do something like (change "DropDown1" to the name of your 下拉列表框):

if (!IsPostBack)
{
   String scriptString = "<script language=JavaScript>function window.onload(){document.all('DropDown1').selectedIndex=-1;}<";
scriptString += "/";
scriptString +="script>";
             this.RegisterClientScriptBlock("clientScript", scriptString);
}

#4


来自在线帮助的信息让我好生失望...
哎,各位说的好象都不行耶...我看了.net的在线帮助,发现这么一句“ An item is always selected in the DropDownList control. You cannot deselect every item in the list at the same time.”似乎我的愿望是不可实现的了...
不过还是要谢谢大家的帮助!
——————————————————

[C#]
public override int SelectedIndex {get; set;}

Property Value
The index of the selected item in the DropDownList control. The default value is 0, which selects the first item in the list.

Remarks
Use the SelectedIndex property to programmatically specify or determine the index of the selected item from the DropDownList control. An item is always selected in the DropDownList control. You cannot deselect every item in the list at the same time.

Note   The indexes for the items in the DropDownList control are zero-based.

#5


你可以换个事件,不使用它的SelectChange(),而是使用Click(),那么,你点击第一个选项,尽管没有改变选择,一样会激活事件的。

#6


DropDownList控件根本就没有Click事件!

#7


if you had tried my method, you should have known it worked

#8


原来如彼!成功实现!看来我的分没有白给啊!呵呵...

#1


将其DragDown属性设置一下,有好多选项。

#2


in Page_Load after possible initialization, try to do

if (!IsPostBack)
   List1.SelectedIndex = -1;

#3


sorry, I was wrong, do something like (change "DropDown1" to the name of your 下拉列表框):

if (!IsPostBack)
{
   String scriptString = "<script language=JavaScript>function window.onload(){document.all('DropDown1').selectedIndex=-1;}<";
scriptString += "/";
scriptString +="script>";
             this.RegisterClientScriptBlock("clientScript", scriptString);
}

#4


来自在线帮助的信息让我好生失望...
哎,各位说的好象都不行耶...我看了.net的在线帮助,发现这么一句“ An item is always selected in the DropDownList control. You cannot deselect every item in the list at the same time.”似乎我的愿望是不可实现的了...
不过还是要谢谢大家的帮助!
——————————————————

[C#]
public override int SelectedIndex {get; set;}

Property Value
The index of the selected item in the DropDownList control. The default value is 0, which selects the first item in the list.

Remarks
Use the SelectedIndex property to programmatically specify or determine the index of the selected item from the DropDownList control. An item is always selected in the DropDownList control. You cannot deselect every item in the list at the same time.

Note   The indexes for the items in the DropDownList control are zero-based.

#5


你可以换个事件,不使用它的SelectChange(),而是使用Click(),那么,你点击第一个选项,尽管没有改变选择,一样会激活事件的。

#6


DropDownList控件根本就没有Click事件!

#7


if you had tried my method, you should have known it worked

#8


原来如彼!成功实现!看来我的分没有白给啊!呵呵...