c# - .Net Gridview 在鼠标悬停后恢复交替颜色

标签 c# gridview

我有一个设置了 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/

相关文章:

c# - 从另一个项目手动在 AspNetUser 表中添加新用户

c# - Azure 函数 - 我们什么时候需要使用 out 进行输出绑定(bind)?

c# - 使用 OWIN 的网络套接字

c# - 在 C# oop 中如何解释为什么基类可以采用派生类实例

asp.net - 获取Gridview单元格的标题文本

c# - 错误 : "an object reference is required for the non-static field, method or property..."

javascript - 单击“保存”按钮时如何验证 gridview?

java - 如何将字符串数组翻译为另一种语言?

c# - 有关更新行数据不起作用的功能

java - 从 SD 卡获取图像时出现 FileNotFoundException