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

时间:2022-01-14 20:10:06

How to SELECT a drop down list item by value programatically in C#.NET?

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

10 个解决方案

#1


78  

If you know that the dropdownlist contains the value you're looking to select, use:

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

ddl.SelectedValue = "2";

If you're not sure if the value exists, use (or you'll get a null reference exception):

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

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

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

#2


23  

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

#3


6  

ddl.SetSelectedValue("2");

With a handy extension:

方便的扩展:

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;
    }
}

Note: Any code is released into the public domain. No attribution required.

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

#4


1  

combobox1.SelectedValue = x;

I suspect you may want yo hear something else, but this is what you asked for.

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

#5


1  

This is a simple way to select an option from a dropdownlist based on a string val

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

#6


1  

Ian Boyd (above) had a great answer -- Add this to Ian Boyd's class "WebExtensions" to select an item in a dropdownlist based on text:

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;
    }

To call it:

打电话给它:

WebExtensions.SetSelectedText(MyDropDownList, "MyValue");

#7


0  

For those who come here by search (because this thread is over 3 years old):

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

string entry // replace with search value

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

#8


0  

Take a look at this article

看看这篇文章

How to select an item in a DropDownList by value Asp.Net

如何通过值Asp.Net选择DropDownList中的项目

#9


0  

I prefer

我更喜欢

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

Replace ddl with the dropdownlist ID and string with your string variable name or value.

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

#10


0  

ddlPageSize.Items.FindByValue("25").Selected = true;

ddlPageSize.Items.FindByValue(“25”)。Selected = true;

#1


78  

If you know that the dropdownlist contains the value you're looking to select, use:

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

ddl.SelectedValue = "2";

If you're not sure if the value exists, use (or you'll get a null reference exception):

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

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

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

#2


23  

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

#3


6  

ddl.SetSelectedValue("2");

With a handy extension:

方便的扩展:

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;
    }
}

Note: Any code is released into the public domain. No attribution required.

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

#4


1  

combobox1.SelectedValue = x;

I suspect you may want yo hear something else, but this is what you asked for.

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

#5


1  

This is a simple way to select an option from a dropdownlist based on a string val

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

#6


1  

Ian Boyd (above) had a great answer -- Add this to Ian Boyd's class "WebExtensions" to select an item in a dropdownlist based on text:

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;
    }

To call it:

打电话给它:

WebExtensions.SetSelectedText(MyDropDownList, "MyValue");

#7


0  

For those who come here by search (because this thread is over 3 years old):

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

string entry // replace with search value

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

#8


0  

Take a look at this article

看看这篇文章

How to select an item in a DropDownList by value Asp.Net

如何通过值Asp.Net选择DropDownList中的项目

#9


0  

I prefer

我更喜欢

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

Replace ddl with the dropdownlist ID and string with your string variable name or value.

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

#10


0  

ddlPageSize.Items.FindByValue("25").Selected = true;

ddlPageSize.Items.FindByValue(“25”)。Selected = true;