How to SELECT a dropdown list item by value programmatically

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

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

10条回答
  •  陌清茗
    陌清茗 (楼主)
    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;
    }
    

提交回复
热议问题