学习拾遗--用RadioButtonList 或者DropDownList 进行动态赋值问题
来源:互联网 发布:淘宝pid获取 编辑:程序博客网 时间:2024/04/30 16:56
动态的给radiobuttonlist 或者dropdownlist 赋值时 如果所赋的数值 在控件的列表中不存在时 往往会发生异常System.ArgumentOutOfRangeException: “DropDownList1”有一个无效 SelectedValue,因为它不在项目列表中)
例如 dropdownlist1中用"111,222,333",如果复制dropdownlist1.text="444"dropdownlist1.selectedvalue="444"时就会出现前面的异常。
但是有时候这种异常没有出现,那是因为我们没有用到dropdownlist或radiobuttonlist控件的(IsPostBack)indexchange事件,当控件需要事件时上面的错误异常就会被捕获。这是因为SelectedValue 属性还可以用于选择列表控件中的某一项,方法是用该项的值设置此属性。如果列表控件中的任何项都不包含指定值,则会引发 System.ArgumentOutOfRangeException。
同样这样 if ( DropDownList1.Items.FindByValue("中国").Selected)也会出现异常。
我们可以用selectedindex来解决上面的问题:
例如:DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("中国"));
这种方法在动态的赋值时先要检索控件列表中是否有匹配的数值,如果数据匹配则dropdownlist.text的值被赋为:“中国”,
如果通过FindByValue没有找到指定项则为null,而Items.IndexOf(null)会返回-1,
如果我们在初始化控件列表时制定了index最小时的默认值(DropDownList1.Items.Insert(0, "请选择"))dropdownlist.text的值被赋为index最小是的值“请选择”,
这样就避免了异常的发生。
- 学习拾遗--用RadioButtonList 或者DropDownList 进行动态赋值问题
- 用RadioButtonList 或者DropDownList 进行动态赋值问题
- 转:用RadioButtonList 或者DropDownList 进行动态赋值问题
- ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
- 给Radiobuttonlist赋值
- 用.net实现注册用户使用RadioButtonList,DropDownList与数据库绑定,使用DropDownList实现省市联动
- js操作DropDownList 与 RadioButtonList
- RadioButtonList \DropDownList 的JS访问
- asp.net.DropDownList后台赋值问题
- radiobuttonlist问题
- 在2003中调用动态生成的radiobuttonlist的问题
- 用Dataset实现对Dropdownlist的赋值
- RadioButtonList、DropDownlist 在客端确认更改
- 关于RadioButtonList,CheckBoxList,DropDownList的Selected Bind
- DataSet绑定ListBox、DropDownList、RadioButtonList、CheckBoxList控件
- DataSet绑定ListBox、DropDownList、RadioButtonList、CheckBoxList控件
- 关于dropdownlist中动态添加Option问题
- 关于动态生成DropDownList遇到的问题
- 理解矩阵(三)
- 学习安装TFS
- 不能显示所有文件
- PHP连接MySQL数据库及以表格输出数据库数据的实现
- 三键教你秘密隐藏重要文档
- 学习拾遗--用RadioButtonList 或者DropDownList 进行动态赋值问题
- SOLOT的开发日记
- Delphi代码获取网卡物理地址
- C#Word转Html的类
- flash页面出现雪花
- 设计模式(2): 什么是设计模式
- 外壳扩展创建快捷方式和获取快捷方式的目标对象
- 明天有SUN的讲座
- 页面出现星星