I am working on an Asp.NET project and I am trying to set the selected value of a dropdown list with a text property. For example i have i.e an item in the dropdown list with text test
. Programmatically can i set it to selecteditem
by Text
?. I am using the follwing code but is not working.
protected void Page_Load(object sender, EventArgs e)
{
string t = "test";
drpFunction.Text = t;
}
But is not working. Any suggestions ?
I think the SelectedValue property should do what you need.
This is better way to select text. By ioden's way it will show an error
Setting the itm.Selected = true; only works if you drp.ClearSelection() first. I prefer the following: