我有一个设置了 alternatingRowStyle 属性的 gridview。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2" OnRowDataBound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged" AlternatingRowStyle-BackColor="#f0f1f3">
我还想在光标移动时突出显示行:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
我遇到的问题是,当鼠标移过该行时,它会恢复为白色,而不是之前的颜色,这在一半的行中是不同的。我想我可以在为每个“onmouseove”事件替换它之前保存当前的行颜色,但是如果快速的鼠标移动会把事情搞砸的话,这似乎很昂贵而且令人担忧。
我没有看到 gridview 行的属性来告诉我它是否是交替行,但是在这里对 rowindex 进行简单的奇数/偶数确定是最好的吗?
有更好的建议吗?
谢谢。
-丹
最佳答案
存储原始样式。然后将样式 backgroundColor 设置为 this.originalstyle
。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
e.Row.Attributes.Add("style", "cursor:pointer;");
关于c# - .Net Gridview 在鼠标悬停后恢复交替颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4597632/