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

时间:2009-08-08 17:15:54

标签: c# .net drop-down-menu

如何在C#.NET中以编程方式按值选择下拉列表项?

11 个答案:

答案 0 :(得分:80)

如果您知道下拉列表包含您要选择的值,请使用:

ddl.SelectedValue = "2";

如果您不确定该值是否存在,请使用(或者您将获得空引用异常):

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

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

答案 1 :(得分:24)

请尝试以下:

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

答案 2 :(得分: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;
    }
}
  

注意:任何代码都会发布到公共域中。无需归属。

答案 3 :(得分:1)

combobox1.SelectedValue = x;

我怀疑你可能想听别的东西,但这就是你要求的。

答案 4 :(得分:1)

这是一种基于字符串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;
            }
        }
    }

答案 5 :(得分:1)

Ian Boyd(上图)有一个很好的答案 - 将此添加到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");

答案 6 :(得分:1)

如果其他人正在尝试此方法并面临问题,那么让我指出一个可能的问题:如果您正在使用Web应用程序,则在 Page_Load 中,如果您正在从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";

希望它可以解决问题

答案 7 :(得分:0)

对于那些通过搜索来到这里的人(因为这个帖子超过3年):

string entry // replace with search value

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

答案 8 :(得分:0)

答案 9 :(得分:0)

我更喜欢

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

将ddl替换为下拉列表ID,并使用字符串变量名称或值替换字符串。

答案 10 :(得分:0)

ddlPageSize.Items.FindByValue(&#34; 25&#34;)。Selected = true;

相关问题