关于DropDownList的怪问题

      DropDownList数据绑时,设置某个项被择,代码如下:
None.gif ddlMagazine.Items.FindByValue(_detail[ 0 ].MagazineCode).Selected  =   true ; // 一种Value查找
None.gif
ddlMagazine.Items.FindByText(Doker.Biz.Magazine.MagazineRule.GetMagazineName(_detail[ 0 ].MagazineCode)).Selected  =   true ; // 另一种Text查找
运行如上代码时,ddlMagazine已经绑定了数据。
运行时出现以上错误:

Cannot have multiple items selected in a DropDownList.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Cannot have multiple items selected in a DropDownList.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.




Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

解决的办法有两种:
      1.DropDownList数据绑时,设置某个项被择前,加如下代码:
None.gif ddlMagazine.ClearSelection();

      设置某项被选择的所以代码如下:
None.gif ddlMagazine.ClearSelection(); // 清除当前的选择
None.gif
                            ddlMagazine.Items.FindByValue(_detail[ 0 ].MagazineCode).Selected  =   true ; // 设置选择例

      2.另一种通过SelectedIndex设置被选择的项,代码如下:
None.gif ddlMagazine.SelectedIndex  =  ddlMagazine.Items.IndexOf(ddlMagazine.Items.FindByValue(_detail[ 0 ].MagazineCode));
以上两种方法都是比较常见绑定 DropDownList的某项被选择!

DropDownList绑定时,默认的是选择第一项为选择项,ddlMagazine.ClearSelection()可以清除选择项,然后重新设置选择项。

转载于:https://www.cnblogs.com/yamajia/archive/2007/04/17/716503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值