首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

关于dropdownlist 的有关问题

2012-04-21 
关于dropdownlist 的问题C# codetry{this.DropDownList6.SelectedValue usertable.Rows[0][姓名].ToSt

关于dropdownlist 的问题

C# code
            try            {                this.DropDownList6.SelectedValue = usertable.Rows[0]["姓名"].ToString();            }            catch            {                this.Label1.Text = usertable.Rows[0]["姓名"].ToString();            }


为什么我这样写的时候还是报错?如何才能不让dropdownlist报错?
报错如下:

“DropDownList6”有一个无效 SelectedValue,因为它不在项目列表中。
参数名: value

[解决办法]
你把usertable.Rows[0]["姓名"].ToString()添加到DropDownList里面了吗?
[解决办法]
usertable.Rows[0]["姓名"].ToString()不在DropDownList6的Items里面


this.DropDownList6.SelectedItem.Selected = false;

for(int i = 0 ; i < this.DropDownList6.Items.Count;i ++)
if(this.DropDownList6.Items[i].Value = = usertable.Rows[0]["姓名"].ToString())
this.DropDownList6.Items[i].Selected = true;

上面的代码更合理些.不过你从数据库里面读出来的值不存在.....你要注意!

热点排行