如何在C#.NET中以編程方式按值選擇下拉列表項目?如何以編程方式按值選擇下拉列表項目
回答
如果您知道下拉列表包含您想選擇的價值,用途:
ddl.SelectedValue = "2";
如果你不知道,如果該值存在,使用(或你會得到空引用除外):
ListItem selectedListItem = ddl.Items.FindByValue("2");
if (selectedListItem != null)
{
selectedListItem.Selected = true;
}
這裏是相同的第二個解決方案,但在一行代碼中:ddl.Items.FindByValue(「2」)。Selected = true; –
如果找不到該項目,將會導致錯誤。 – ScottE
下拉列表位於已加載頁面的彈出窗口中。我試圖在顯示彈出窗口之前設置選定的索引,但它不起作用。你能幫忙嗎? http://stackoverflow.com/questions/28883433/how-to-populate-dropdownlist-in-a-popup-before-showing-from-code-behind – SearchForKnowledge
combobox1.SelectedValue = x;
我懷疑你可能想要喲聽到別的,但這是你要求的。
你不能,因爲選定的值只能得到沒有設置的值 –
David - 你實際上是不正確的。試試看,如果你給-1,你應該刪除它!如果這個值不存在,你當然會得到一個異常,但是它可以正常工作。 – ScottE
myDropDown.SelectedIndex =
myDropDown.Items.IndexOf(myDropDown.Items.FindByValue("myValue"))
該值在myDropDown.SelectedIndex中變爲-1爲什麼? –
可能是因爲myDropDown.Items沒有項目「myValue」 –
如果項目不在集合中,IndexOf()返回-1。 FindByValue()找不到要查找的項目。如果您需要調試它,請將它分解成單獨的語句。 – womp
這是一個簡單的方法來選擇基於字符串VAL
從下拉列表的選項ddl.SetSelectedValue("2");
了一個方便的擴展:
public static class WebExtensions
{
/// <summary>
/// Selects the item in the list control that contains the specified value, if it exists.
/// </summary>
/// <param name="dropDownList"></param>
/// <param name="selectedValue">The value of the item in the list control to select</param>
/// <returns>Returns true if the value exists in the list control, false otherwise</returns>
public static Boolean SetSelectedValue(this DropDownList dropDownList, String selectedValue)
{
ListItem selectedListItem = dropDownList.Items.FindByValue(selectedValue);
if (selectedListItem != null)
{
selectedListItem.Selected = true;
return true;
}
else
return false;
}
}
注意:任何代碼發佈到公共領域。無需歸屬。
對於那些誰到這裏來被搜索(因爲此線程是3歲以上):
string entry // replace with search value
if (comboBox.Items.Contains(entry))
comboBox.SelectedIndex = comboBox.Items.IndexOf(entry);
else
comboBox.SelectedIndex = 0;
Contains方法需要一個ListItem參數,而不是一個字符串值參數。 –
伊恩·博伊德看看(以上)有一個很好的答案 - 將此添加到Ian Boyd的課程「WebExtensions」中,根據文本在下拉列表中選擇一個項目:
/// <summary>
/// Selects the item in the list control that contains the specified text, if it exists.
/// </summary>
/// <param name="dropDownList"></param>
/// <param name="selectedText">The text of the item in the list control to select</param>
/// <returns>Returns true if the value exists in the list control, false otherwise</returns>
public static Boolean SetSelectedText(this DropDownList dropDownList, String selectedText)
{
ListItem selectedListItem = dropDownList.Items.FindByText(selectedText);
if (selectedListItem != null)
{
selectedListItem.Selected = true;
return true;
}
else
return false;
}
要叫它:
WebExtensions.SetSelectedText(MyDropDownList, "MyValue");
- 1. 以編程方式選擇kendo下拉列表中的值
- 2. 如何以編程方式使用jquery按值在下拉列表中選擇項目?
- 3. 以編程方式選擇下一個列表框項目
- 4. 如何在C#中以編程方式按值選擇下拉列表項並將其傳遞給整數?
- 5. 在下拉列表中選擇一個選項<select>以編程方式
- 6. 以編程方式選擇下拉選項
- 7. 以編程方式從列表中選擇列表框中的項目值
- 8. 選擇項目以編程方式UICollectionView
- 9. NSMenu以編程方式選擇項目
- 10. 如何以編程方式設置jQuery下拉列表的值?
- 11. 以編程方式選擇Dashcode列表中的項目
- 12. 如何以編程方式選擇下拉列表中的jQuery Mobile的
- 13. 如何從gridview中選擇項目形式的下拉列表
- 14. 選擇下拉列表中的已選項目/選擇列表
- 15. SSRS 2008 R2:以編程方式設置下拉選擇值
- 16. 選擇的項目 - 下拉列表 - SQL
- 17. 下拉列表選擇的項目
- 18. 下拉列表後,新項目選擇
- 19. 如何以編程方式在列表視圖中選擇項目
- 20. 選擇下拉值編程
- 21. 如何更新選擇的下拉列表項目(級聯下拉列表)?
- 22. 如何以編程方式在collectionview中選擇項目?
- 23. 如何以編程方式選擇圖庫的特定項目?
- 24. 如何以編程方式選擇ListView中的項目?
- 25. 如何在vega中以編程方式選擇項目?
- 26. 如何以編程方式選擇ListView項目
- 27. DataGrid:如何以編程方式選擇ComboBox項目?
- 28. 如何以編程方式選擇指定的ListBox項目?
- 29. 如何以編程方式選擇項目時觸發ComboBox EventHandler?
- 30. 如何選擇下拉列表值?
正在尋找一種方法來使用自動化(例如測試)來做到這一點? – Steven
這是用於WinForms,WPF,網頁嗎? –
沒有我只是爲了選擇國家的價值取決於我在數據庫中的值 –