protected void Page_Load(object sender, EventArgs e)

...{
if (!IsPostBack)

...{
ViewState["LineNo"] = 0; //0为奇数行,1为偶数行
}
}

//数据行建立事件
protected void gviewEmployees_RowDataBound(object sender, GridViewRowEventArgs e)

...{
switch (e.Row.RowType)

...{
case DataControlRowType.Header:
e.Row.BackColor = Color.FromArgb(153, 0, 0);
e.Row.ForeColor = Color.White;
break;
case DataControlRowType.DataRow:
//建立奇数行与偶数行的onmouseover及onmouseout的颜色变换
if (Convert.ToInt16(ViewState["LineNo"]) == 0)

...{
e.Row.BackColor = Color.FromArgb(255, 251, 214);
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFBD6';this.style.color='black'");
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#C0C0FF';this.style.color='#ffffff'");

ViewState["LineNo"] = 1;
}
else

...{
e.Row.BackColor = Color.White;
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF';this.style.color='black'");
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#C0C0FF';this.style.color='#ffffff'");

ViewState["LineNo"] = 0;
}

break;
}
}

...{
if (!IsPostBack)
...{
ViewState["LineNo"] = 0; //0为奇数行,1为偶数行
}
}
//数据行建立事件
protected void gviewEmployees_RowDataBound(object sender, GridViewRowEventArgs e)
...{
switch (e.Row.RowType)
...{
case DataControlRowType.Header:
e.Row.BackColor = Color.FromArgb(153, 0, 0);
e.Row.ForeColor = Color.White;
break;
case DataControlRowType.DataRow:
//建立奇数行与偶数行的onmouseover及onmouseout的颜色变换
if (Convert.ToInt16(ViewState["LineNo"]) == 0)
...{
e.Row.BackColor = Color.FromArgb(255, 251, 214);
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFBD6';this.style.color='black'");
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#C0C0FF';this.style.color='#ffffff'");
ViewState["LineNo"] = 1;
}
else
...{
e.Row.BackColor = Color.White;
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF';this.style.color='black'");
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#C0C0FF';this.style.color='#ffffff'");
ViewState["LineNo"] = 0;
}
break;
}
}
本文介绍如何在ASP.NET中使用GridView控件并实现数据行的奇偶行不同背景颜色显示效果,包括鼠标悬停时的颜色变化。通过设置ViewState变量来区分奇数行和偶数行,并为不同的行类型应用了特定的样式。
385

被折叠的 条评论
为什么被折叠?



