how to select with DropDownList.text

2020-02-23 04:39发布

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 ?

9条回答
\"骚年 ilove
2楼-- · 2020-02-23 05:31

I think the SelectedValue property should do what you need.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-23 05:37
drpFunction.SelectedValue = drpFunction.Items.FindByText(t).Value;

This is better way to select text. By ioden's way it will show an error

"Multiple Items Cannot be selected in DropDownList"

查看更多
Summer. ? 凉城
4楼-- · 2020-02-23 05:38

Setting the itm.Selected = true; only works if you drp.ClearSelection() first. I prefer the following:

drpFunction.SelectedValue = drpFunction.Items.FindByText(t).Value;
查看更多
登录 后发表回答