我有一个ASP的DropDownList,其中添加了一些项。我想要做的就是在页面加载后使选择为空,这样就没有任何选中的项了。
我应该怎么做呢?
我有一个ASP的DropDownList,其中添加了一些项。我想要做的就是在页面加载后使选择为空,这样就没有任何选中的项了。
我应该怎么做呢?
你可以通过编程的方式向下拉列表的顶部添加一个空项,代码如下:
myDropDown.Items.Insert(0, new ListItem(string.Empty, string.Empty));
myDropDown.SelectedIndex = 0;
我不确定我理解你的问题,但是尝试这样做:
DropDownList1.ClearSelection()
或者
DropDownList1.SelectedIndex = -1;
如果下拉列表是通过数据源填充的,那么在插入之前进行数据绑定非常重要。否则,项目插入将不会发生。
myDropDown.DataBind();
myDropDown.Items.Insert(0, new ListItem(string.Empty, string.Empty));
myDropDown.SelectedIndex = 0;
<asp:DropDownList ID="YourID" runat="server" DataSourceID="YourDataSource
DataTextField="Text" DataValueField="Value" AppendDataBoundItems="True">
<asp:ListItem Text="" Selected="True"></asp:ListItem>
</asp:DropDownList>
您可以将SelectedIndex
属性设置为-1,或者在数据源中添加一个空条目作为第一项,并在表单提交时验证选择。
yourDropDownList.Items.Clear()
为了重新填充,您可以按照 womps 的建议静态地添加项目(在insert()
方法中替换参数),或者您可以从数据源动态填充。
列表项的后备存储是 ListItemCollection。