您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

保持dropdownlist选中值

作者:佚名    责任编辑:admin    更新时间:    2015-10-14 17:57:44

 RowDataBound事件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i;


for (i = 0; i < GridView1.Rows.Count; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFFCC'");

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

}
}
e.Row.Attributes["style"] = "Cursor:hand";

 

//问题分类下拉框绑定选中值
if (((DropDownList)e.Row.FindControl("ddlProVar")) != null)
{
DropDownList ddlProVar = (DropDownList)e.Row.FindControl("ddlProVar");

SetSelectedItem(ddlProVar, ((HiddenField)e.Row.FindControl("hdProVarState")).Value);
}

}

 

/// <summary>
/// 保存选定的dropdownlist值
/// </summary>
/// <param name="list"></param>
/// <param name="value"></param>
private void SetSelectedItem(System.Web.UI.WebControls.ListControl list, String value)
{
foreach (ListItem e in list.Items)
{
if (e.Text == value)
{
e.Selected = true;
return;
}
else e.Selected = false;
}
}