如何通过编程的价值选择下拉列表项 [英] How to SELECT a dropdown list item by value programatically

查看:164
本文介绍了如何通过编程的价值选择下拉列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择下拉列表中的项目按值编程在C#.NET?

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

推荐答案

如果您知道的DropDownList包含你正在寻找指定的值,使用:

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;
}

这篇关于如何通过编程的价值选择下拉列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆