萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> 保持dropdownlist選中值

保持dropdownlist選中值

 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;
}
}

copyright © 萬盛學電腦網 all rights reserved