ASP.net DropDownList with no selected item

2019-02-16 11:27发布

I have a ASP DropDownList with items added to it. All what I want is to make the selection after the page loaded empty so there is no a selected item.

How can I do that?

6条回答
闹够了就滚
2楼-- · 2019-02-16 12:06

You can set the SelectedIndex property to -1 or you can add an empty entry as the first item in the data source and validate the selection on form submission.

查看更多
冷血范
3楼-- · 2019-02-16 12:08

This should work on the client side:

<asp:DropDownList ID="YourID" runat="server" DataSourceID="YourDataSource
DataTextField="Text" DataValueField="Value" AppendDataBoundItems="True">
    <asp:ListItem Text="" Selected="True"></asp:ListItem>
</asp:DropDownList>
查看更多
我想做一个坏孩纸
4楼-- · 2019-02-16 12:16

Not sure I understand your question but try this:

DropDownList1.ClearSelection()

or

DropDownList1.SelectedIndex = -1;
查看更多
爷的心禁止访问
5楼-- · 2019-02-16 12:24

If the dropdownlist is being populated by DataSource, is important do the DataBind before the insert. Otherwise the item insertion does not happen.

myDropDown.DataBind();
myDropDown.Items.Insert(0, new ListItem(string.Empty, string.Empty));
myDropDown.SelectedIndex = 0;

https://stackoverflow.com/a/2224636/1467453

查看更多
女痞
6楼-- · 2019-02-16 12:26

You can add an empty item to the top of your dropdownlist programmatically like this:

myDropDown.Items.Insert(0, new ListItem(string.Empty, string.Empty));
myDropDown.SelectedIndex = 0;
查看更多
我只想做你的唯一
7楼-- · 2019-02-16 12:30

yourDropDownList.Items.Clear()

To repopulate, you can either add items statically as per womps suggestion (substituting params in the insert() method, or you can populate it dynamically from a data source. The backing store for list items is the ListItemCollection.

查看更多
登录 后发表回答