Setting dropdownlist selecteditem programmatically
Asked Answered
H

12

75

I want to set the selecteditem attribute for an ASP.Net dropdownlist control programmatically.

So I want to pass a value to the dropdownlist control to set the selected item where the item is equal to the passed value.

Holmquist answered 16/8, 2010 at 19:12 Comment(1)
Do you mean myList.SelectedValue = someStringValue;?Messer
R
100

Assuming the list is already data bound you can simply set the SelectedValue property on your dropdown list.

list.DataSource = GetListItems(); // <-- Get your data from somewhere.
list.DataValueField = "ValueProperty";
list.DataTextField = "TextProperty";
list.DataBind();

list.SelectedValue = myValue.ToString();

The value of the myValue variable would need to exist in the property specified within the DataValueField in your controls databinding.

UPDATE: If the value of myValue doesn't exist as a value with the dropdown list options it will default to select the first option in the dropdown list.

Rainey answered 16/8, 2010 at 19:17 Comment(2)
No, SelectedValue is not read-only: "Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value."Zeebrugge
This did just work for me, it chose which element should be pre-selected base on what value I gave .SelectedValue.Homozygous
J
63

ddlData.SelectedIndex will contain the int value To select the specific value into DropDown :

ddlData.SelectedIndex=ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));

return type of ddlData.Items.IndexOf(ddlData.Items.FindByText("value")); is int.

Jalisajalisco answered 17/8, 2010 at 5:23 Comment(0)
H
28

Here is the code I was looking for :

DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByText("PassedValue"));

Or

DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByValue("PassedValue"));
Holmquist answered 16/8, 2010 at 20:12 Comment(1)
That's basically doing the same thing with more code. Even if the selected value doesn't exist as an item in the list it defaults to the first option as its selected value.Rainey
W
5

Well if I understood correctly your question. The Solution for setting the value for a given dropdownlist will be:

dropdownlist1.Text="Your Value";

This will work only if the value is existing in the data-source of the dropdownlist.

Washout answered 9/5, 2013 at 4:13 Comment(0)
T
4

If you need to select your list item based on an expression:

foreach (ListItem listItem in list.Items)
{
    listItem.Selected = listItem.Value.Contains("some value");
}
Throat answered 2/1, 2014 at 23:36 Comment(0)
K
3

Just Use this oneliner:

divisions.Items.FindByText("Some Text").Selected = true;
divisions.Items.FindByValue("some value").Selected = true;

where divisions is a dropdownlist control.

Hope it helps someone.

Keshiakesia answered 5/12, 2019 at 8:11 Comment(1)
You helped me , thanks you very much for thisFidelia
S
1
var index = ctx.Items.FirstOrDefault(item => Equals(item.Value, Settings.Default.Format_Encoding));
ctx.SelectedIndex = ctx.Items.IndexOf(index);

OR

foreach (var listItem in ctx.Items)
  listItem.Selected = Equals(listItem.Value as Encoding, Settings.Default.Format_Encoding);

Should work.. especially when using extended RAD controls in which FindByText/Value doesn't even exist!

Switcheroo answered 17/12, 2014 at 23:30 Comment(0)
F
1
ddList.Items.FindByText("oldValue").Selected = false;
ddList.Items.FindByText("newValue").Selected = true;
Frenchpolish answered 21/12, 2016 at 11:10 Comment(1)
Welcome to StackOverflow. To make your answer bett - add some comments to a code you posted. Posting a block of code without any explanations is not a proper answer.Caron
S
0

On load of My Windows Form the comboBox will display the ClassName column of my DataTable as it's the DisplayMember also has its ValueMember (not visible to user) with it.

private void Form1_Load(object sender, EventArgs e)
            {
                this.comboBoxSubjectCName.DataSource = this.Student.TableClass;
                this.comboBoxSubjectCName.DisplayMember = TableColumn.ClassName;//Column name that will be the DisplayMember
                this.comboBoxSubjectCName.ValueMember = TableColumn.ClassID;//Column name that will be the ValueMember
            }
Stegall answered 27/8, 2018 at 7:2 Comment(0)
L
0

Safety check to only select if an item is matched.

//try to find item in list.  
ListItem oItem = DDL.Items.FindByValue("PassedValue"));
//if exists, select it.
if (oItem != null) oItem.Selected = true;
Leninism answered 23/9, 2018 at 4:58 Comment(0)
U
0

i think this is the best solution

drpGroups.SelectedValue = drpGroups.Items[0].Value.ToString();

Unguarded answered 25/12, 2023 at 13:4 Comment(0)
H
-1
            ddlemployee.DataSource = ds.Tables[0];
            ddlemployee.DataTextField = "Employee Name";
            ddlemployee.DataValueField = "RecId";
            ddlemployee.DataBind();
            ddlemployee.Items.Insert(0, "All");
Hammons answered 28/7, 2015 at 20:1 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.