转:用RadioButtonList 或者DropDownList 进行动态赋值问题
来源:互联网 发布:薛之谦 淘宝 店铺名称 编辑:程序博客网 时间:2024/05/02 00:02
http://blog.csdn.net/huomm/article/details/1865404
动态的给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最小是的值“请选择”,
这样就避免了异常的发生。
0 0
- 转:用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遇到的问题
- 照片解禁了,中國人的心何日解禁 ?-----永不 ! (图片惨无人道,慎入)
- ASP.NET中如何防范SQL注入式攻击
- 正则表达式语法(转)
- 防止SQL注入-参数化SQL例代码asp.net
- Jenkins Error: android: $GERRIT_REFSPEC" returned status code 128
- 转:用RadioButtonList 或者DropDownList 进行动态赋值问题
- 常用颜色的RGB值和中文名字
- 压缩文件格式.RAR和Zip的详细区别!
- winxp将Administrator账户显示在欢迎屏幕
- Android动画背景图自动播放的实现
- 观赏鱼常见病防治
- 百度收录问题更新2则
- ASPNETDB.MDF合并到自己的数据库
- joomla