ASP.net DropDownList with no selected item

◇◆丶佛笑我妖孽 提交于 2019-12-19 05:43:46

问题


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?


回答1:


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;



回答2:


Not sure I understand your question but try this:

DropDownList1.ClearSelection()

or

DropDownList1.SelectedIndex = -1;



回答3:


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.




回答4:


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>



回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/2224611/asp-net-dropdownlist-with-no-selected-item

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