2016-10-26 77 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時,以上適用。其餘的將被清除。