读取数据库中值,将其设为select(下拉)控件选中值
代码
#region 读取数据库中值,将其设为select(下拉)控件选中值
//法一:
foreach (ListItem li in sel_Department.Items)
{
if (li.Value.Trim() == sdr["Flag"].ToString().Trim())
{
li.Selected = true;
break;
}
}
//法二:
ListItem li = sel_Department.Items.FindByValue(sdr["Flag"].ToString().Trim());
//法一:
foreach (ListItem li in sel_Department.Items)
{
if (li.Value.Trim() == sdr["Flag"].ToString().Trim())
{
li.Selected = true;
break;
}
}
//法二:
ListItem li = sel_Department.Items.FindByValue(sdr["Flag"].ToString().Trim());
if (li != null) li.Selected = true;//防止上语句FindByValue方法返回null对象
#endregion
#endregion