How to SELECT a dropdown list item by value programmatically

前端 未结 10 526
滥情空心
滥情空心 2020-12-13 00:11

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

相关标签:
10条回答
  • 2020-12-13 00:28
    combobox1.SelectedValue = x;
    

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

    0 讨论(0)
  • 2020-12-13 00:31
    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.

    0 讨论(0)
  • 2020-12-13 00:32

    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:

    /// <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");
    
    0 讨论(0)
  • 2020-12-13 00:33

    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;
    }
    
    0 讨论(0)
  • 2020-12-13 00:34

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

    0 讨论(0)
  • 2020-12-13 00:40

    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.

    0 讨论(0)
提交回复
热议问题