当前位置: 代码迷 >> ASP.NET >> 关于dropdownlist 的有关问题
  详细解决方案

关于dropdownlist 的有关问题

热度:8161   发布时间:2013-02-25 00:00:00.0
关于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;

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