ddlMagazine.Items.FindByValue(_detail[
0
].MagazineCode).Selected
=
true
;
//
一种Value查找
ddlMagazine.Items.FindByText(Doker.Biz.Magazine.MagazineRule.GetMagazineName(_detail[ 0 ].MagazineCode)).Selected = true ; // 另一种Text查找
运行如上代码时,ddlMagazine已经绑定了数据。
ddlMagazine.Items.FindByText(Doker.Biz.Magazine.MagazineRule.GetMagazineName(_detail[ 0 ].MagazineCode)).Selected = true ; // 另一种Text查找
运行时出现以上错误:
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数据绑时,设置某个项被择前,加如下代码:
ddlMagazine.ClearSelection();
设置某项被选择的所以代码如下:
ddlMagazine.ClearSelection();
//
清除当前的选择
ddlMagazine.Items.FindByValue(_detail[ 0 ].MagazineCode).Selected = true ; // 设置选择例
ddlMagazine.Items.FindByValue(_detail[ 0 ].MagazineCode).Selected = true ; // 设置选择例
2.另一种通过SelectedIndex设置被选择的项,代码如下:
ddlMagazine.SelectedIndex
=
ddlMagazine.Items.IndexOf(ddlMagazine.Items.FindByValue(_detail[
0
].MagazineCode));
以上两种方法都是比较常见绑定
DropDownList的某项被选择!
DropDownList绑定时,默认的是选择第一项为选择项,ddlMagazine.ClearSelection()可以清除选择项,然后重新设置选择项。