悬停时如何保留原始背景色



下面的代码工作得很好,但我唯一的问题是:它覆盖了backgroundColor='white'的替代行,我怎么能有我原来的替代颜色时onmouseout ?

<AlternatingRowStyle BackColor="#DEEEE9" Font-Size="8pt" />
if (e.Row.RowType == DataControlRowType.DataRow)
{
    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#C2D69B'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
    e.Row.Attributes.Add("style", "cursor:pointer;");
}

您可以指定应该在onmouseout上还原什么颜色:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    string bgcolor = "white"
    if (e.Row.RowState == DataControlRowState.Alternate)
    {
       bgcolor = "#DEEEE9";
    }
    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#C2D69B'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + bgcolor  + "'");
    e.Row.Attributes.Add("style", "cursor:pointer;");
}

我不明白,为什么不把"e.Row.Attributes "去掉呢?添加("onmouseout", "this.style.backgroundColor='white'");"并将其设置为原始替代颜色???

使用悬停CSS属性,而不是指定特定的颜色。看到的:http://www.codeproject.com/KB/webforms/MouseHoverUsingCSS.aspx

试试这样:

var color = "<%=System.Drawing.ColorTranslator.ToHtml(GridView1.AlternatingRowStyle.BackColor)%>";

用更少的代码就可以很好地工作。在设置backgroundColor之前,在鼠标上方创建一个自定义属性,并在鼠标移出时使用它。

row.Attributes["onmouseover"] = this.originalstyle=this.style.backgroundColor;this.style.cursor='hand';this.style.backgroundColor='#ffccff';";
row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.backgroundColor=this.originalstyle;";

最新更新