DropDownList绑定数据的问题(未解决)

来源:百度知道 编辑:UC知道 时间:2024/04/30 12:01:05
例子:
我有一篇文章,添加时选择了所属大类:国际新闻(这个类别的ID号是3).
我在添加文章时,存入的classid字段是数字3,也就是代表国际新闻.
现在我修改文章时,怎样在页面载入时,dropdownlist下拉框中默认的选中项是国际新闻?
int cid = Convert.ToInt32(dr["classid"]);//取出数据库的类ID,即classid
for(int i=0;i<DropDownList1.Items.Count;i++)
{
if (Convert.ToInt32(this.DropDownList1.Items[i].Value)== cid)//比较哪一项的值与数据库的相同.
{
this.DropDownList1.SelectedIndex =i; //如果哪一项相同就哪一项为选中状态.
}
}

以上为正确答案.所以楼下的回答均有误,或者过于简便.思路不清.

DropDownList有个selectedvalue属性,该属性是你当前选择的值,你要想把这个值赋给这个属性就OK了,即:DropDownList.SelectedValue = 获取到你所需要的id;

//加载页面时添加
DropDownList1.SelectedValue = "3";

你先设置那个dropdownlist的属性value=classid,然后就像dugubingdian说的一样,在加载时添加dropdownlist.selectedvalue="3";