我有一个设置了alternatingRowStyle属性的网格视图。
<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“事件替换它之前保存当前的rowcolor,但是如果快速的鼠标移动可能会把事情搞乱,这似乎是昂贵和令人担忧的。
我没有看到网格视图行的属性来告诉我它是否是交替行,但是在rowindex上进行一个简单的奇偶判断是最好的吗?
有什么更好的建议吗?
谢谢。
-Dan
发布于 2011-01-04 19:43:26
存储原始样式。然后将样式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;");
发布于 2011-02-23 00:46:18
我更喜欢使用CSS的悬停颜色
tr.odd {
background-color: White;
}
tr.even {
background-color: #f8f8f8;
}
tr.odd:hover {
background-color: #f3fcfa;
}
tr.even:hover {
background-color: #ebf3f1;
}
发布于 2012-10-24 15:44:17
使用JQuery:
$(document).ready(function ()
{
var original = "";
$("#<%=yourGridViewNameHere.ClientID%> tr:has(td)").hover(function ()
{
original = $(this).css("background-color");
$(this).css("background-color", "Pink");
}, function ()
{
$(this).css("background-color", original);
});
});
我从here得到的原始答案,我只是根据我的需要做了一点调整,如上所述。
尽情享受
https://stackoverflow.com/questions/4597632
复制