C# 如何以编程方式按值选择下拉列表项

C# 如何以编程方式按值选择下拉列表项,c#,.net,drop-down-menu,C#,.net,Drop Down Menu,如何在C.NET中按值编程选择下拉列表项 combobox1.SelectedValue = x; 我想你可能想听听别的,但这正是你想要的。请尝试以下内容: myDropDown.SelectedIndex = myDropDown.Items.IndexOf(myDropDown.Items.FindByValue("myValue")) 如果您知道dropdownlist包含要选择的值,请使用: ddl.SelectedValue = "2"; 如果不确定该值是否存在,请使用,否则将

如何在C.NET中按值编程选择下拉列表项

combobox1.SelectedValue = x;
我想你可能想听听别的,但这正是你想要的。

请尝试以下内容:

myDropDown.SelectedIndex = 
myDropDown.Items.IndexOf(myDropDown.Items.FindByValue("myValue"))

如果您知道dropdownlist包含要选择的值,请使用:

ddl.SelectedValue = "2";
如果不确定该值是否存在,请使用,否则将出现空引用异常:

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

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

这是一种基于字符串val从下拉列表中选择选项的简单方法

private void SetDDLs(DropDownList d,string val)
    {
        ListItem li;
        for (int i = 0; i < d.Items.Count; i++)
        {
            li = d.Items[i];
            if (li.Value == val)
            {
                d.SelectedIndex = i;
                break;
            }
        }
    }
有一个方便的扩展:

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;

上面的Ian Boyd有一个很好的答案-将此添加到Ian Boyd的类WebExtensions中,以基于文本在dropdownlist中选择一个项目:

/// <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;
}
我更喜欢

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

用dropdownlist ID替换ddl,用字符串变量名称或值替换字符串。

ddlPageSize.Items.FindByValue25.Selected=true

如果其他人正在尝试此操作并面临问题,那么让我指出一个可能的问题:如果您正在使用Web应用程序,那么在页面内加载如果您正在从DB加载下拉列表,同时您希望加载数据,那么首先加载您的下拉列表,然后使用选定的下拉条件加载数据

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        LoadDropdown(); //drop-downs generated first
        LoadData(); // other data loading and drop-down value selection logic here
    }
}
要成功地从代码中选择下拉列表,请遵循上面的已批准答案,即

ddl.SelectedValue=2


希望它能解决问题

我们正在寻找一种方法,使用自动化来实现这一点,比如说测试?这是针对WinForms、WPF、web的吗?不,我只是根据我在数据库中的值选择国家/地区。可能的重复项是不能的,因为所选值只获取值,而不是设置的值大卫-实际上你是不正确的。试一试,如果你给了-1,你可能应该删除它!如果该值不存在,您当然会遇到异常,但在其他情况下效果很好。该值在myDropDown中变为-1。SelectedIndex为什么?可能是因为myDropDown。如果该项不在集合中,Items没有项myValueIndexOf将返回-1。FindByValue未找到您要查找的项目。如果需要调试,只需将其拆分为单独的语句即可;dropdownlist位于页面加载时填充的弹出窗口中。我试图在显示弹出窗口之前设置所选索引,但它不起作用。你能帮忙吗?Contains方法接受一个ListItem参数,而不是字符串值参数。@真的吗?这或多或少并不意味着与众不同。它到底行不行?它是否与提供的东西相同?如果第一个选择“是”,第二个选择“否”,为什么选择“负号?”@MarioDS答案提供了相同的函数,代码更少。这里的所有答案为不同的好处提供了不同的方式,这一个的好处是更少的代码。这已经发布了多次。你想补充什么?
if(ddl.Items.FindByValue(string) != null)
{
    ddl.Items.FindByValue(string).Selected = true;
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        LoadDropdown(); //drop-downs generated first
        LoadData(); // other data loading and drop-down value selection logic here
    }
}