2009-08-08 31 views
42

如何在C#.NET中以編程方式按值選擇下拉列表項目?如何以編程方式按值選擇下拉列表項目

+0

正在尋找一種方法來使用自動化(例如測試)來做到這一點? – Steven

+1

這是用於WinForms,WPF,網頁嗎? –

+0

沒有我只是爲了選擇國家的價值取決於我在數據庫中的值 –

回答

77

如果您知道下拉列表包含您想選擇的價值,用途:

ddl.SelectedValue = "2"; 

如果你不知道,如果該值存在,使用(或你會得到空引用除外):

ListItem selectedListItem = ddl.Items.FindByValue("2"); 

if (selectedListItem != null) 
{ 
    selectedListItem.Selected = true; 
} 
+3

這裏是相同的第二個解決方案,但在一行代碼中:ddl.Items.FindByValue(「2」)。Selected = true; –

+2

如果找不到該項目,將會導致錯誤。 – ScottE

+0

下拉列表位於已加載頁面的彈出窗口中。我試圖在顯示彈出窗口之前設置選定的索引,但它不起作用。你能幫忙嗎? http://stackoverflow.com/questions/28883433/how-to-populate-dropdownlist-in-a-popup-before-showing-from-code-behind – SearchForKnowledge

1
combobox1.SelectedValue = x; 

我懷疑你可能想要喲聽到別的,但這是你要求的。

+0

你不能,因爲選定的值只能得到沒有設置的值 –

+0

David - 你實際上是不正確的。試試看,如果你給-1,你應該刪除它!如果這個值不存在,你當然會得到一個異常,但是它可以正常工作。 – ScottE

23
myDropDown.SelectedIndex = 
    myDropDown.Items.IndexOf(myDropDown.Items.FindByValue("myValue")) 
+0

該值在myDropDown.SelectedIndex中變爲-1爲什麼? –

+1

可能是因爲myDropDown.Items沒有項目「myValue」 –

+0

如果項目不在集合中,IndexOf()返回-1。 FindByValue()找不到要查找的項目。如果您需要調試它,請將它分解成單獨的語句。 – womp

1

這是一個簡單的方法來選擇基於字符串VAL

從下拉列表的選項
6
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; 
    } 
} 

注意:任何代碼發佈到公共領域。無需歸屬。

0

對於那些誰到這裏來被搜索(因爲此線程是3歲以上):

string entry // replace with search value 

if (comboBox.Items.Contains(entry)) 
    comboBox.SelectedIndex = comboBox.Items.IndexOf(entry); 
else 
    comboBox.SelectedIndex = 0; 
+0

Contains方法需要一個ListItem參數,而不是一個字符串值參數。 –

1

伊恩·博伊德看看(以上)有一個很好的答案 - 將此添加到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"); 
0

我喜歡

if(ddl.Items.FindByValue(string) != null) 
{ 
    ddl.Items.FindByValue(string).Selected = true; 
} 

與下拉列表ID和字符串與字符串變量名稱或值替換DDL。

+0

這個答案已經提供或多或少相同的形式... – MarioDS

+0

@MarioDS真的嗎?沒有更多或更少表示不同。它工作與否?它與提供或不提供的東西是一樣的嗎?如果是第一個和第二個,爲什麼減號? – San

+0

@MarioDS答案以較少的代碼提供相同的功能。這裏的所有答案都爲不同的好處提供了不同的方式,這個好處是代碼少。 – San

相關問題