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.
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.
ddlData.SelectedIndex
will contain theint
value To select the specific value intoDropDown
:return
type ofddlData.Items.IndexOf(ddlData.Items.FindByText("value"));
isint
.OR
Should work.. especially when using extended RAD controls in which FindByText/Value doesn't even exist!
Assuming the list is already data bound you can simply set the
SelectedValue
property on your dropdown list.The value of the
myValue
variable would need to exist in the property specified within theDataValueField
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.Well if I understood correctly your question. The Solution for setting the value for a given dropdownlist will be:
This will work only if the value is existing in the data-source of the dropdownlist.
Safety check to only select if an item is matched.