2016-10-26 73 views
0

我有一个ASP.Net网站为值插入到数据库中,我想重置在下拉列表选择的项目,我该怎么做正确?我知道与文本框,我可以使用TextBox1.Text = String.empty但它似乎与dropdownLists如何删除DropDownList的选定索引?

+0

添加一些代码.. –

+0

请参考[最小,完整的,并且可验证示例](http://stackoverflow.com/help/mcve)。 – Enfyve

+0

请使用以下代码清除列表。 DropDownList1.Items.Clear(); –

回答

1

对于DropDownList工作倒不如先在DropDownList的第一位置使用yourDropDownList.Items.Insert方法添加一个空字符串,然后选择它。事情是这样的:

yourDropDownList.Items.Insert(0, new ListItem(string.Empty, string.Empty)); 
yourDropDownList.SelectedIndex = 0; 
2

如果要清除所有项目意味着你可以使用

aspDrop.Items.Clear(); // whill clear all the list items 

如果要更改选定的指数意味着你必须使用

aspDrop.SelectedIndex = someIndex; // where index must be in the list 

或者甚至你可以用.Insert()方法将项目插入到特定的索引,将它作为选择的项目。

1

那么,如果你想完全清除下拉列表,所以你可以清除它在这样的项目源,例如:

ddItems.Clear(); 

但是如果你只想要清除选定dropdownListItem比S.Akbari解决方案:

ddItems.Items.Insert(0, new ListItem(string.Empty, string.Empty)); 
ddItems.SelectedIndex = 0; 
1

我想,每个人都给出答案。但要记住一件事情要做如下:

AppendDataBoundItems="False" 

将其设置为,因为这将导致反弹将数据添加到不会有约束力之前清除现有列表。当数据绑定到DropDownList时,以上适用。其余的将被清除。