gridview 如何实现鼠标悬停效果,且能实现单击某行,让整行处于选中状态,变为红色。然后,单击其它行,则其它行处于选 中状态,变为红色,刚才那行恢复原来颜色

tiger8000 2012-01-13 02:35:56
gridview 如何实现鼠标悬停效果,且能实现单击某行,让整行处于选中状态,变为红色。然后,单击其它行,则其它行处于选 中状态,变为红色,刚才那行恢复原来颜色


隔行变色 我知道就在 gridview 属行里就可以实现


<RowStyle BackColor="#e6e6fa" Height="22" ForeColor="Black" />
<AlternatingRowStyle BackColor="#f5f5dc" Height="22" ForeColor="Black" />


实样,就能实现gridview 隔行变色了。

但是,不知道如何实现鼠标悬停效果,和 如何实现选 中行变色。我知道应该在哪里实现,但是就是不知道如何俱体写



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
//应该是在这里实现,还请大侠出手帮忙!预祝新年快乐 !!
}

}
...全文
188 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
phhui 2012-01-13
  • 打赏
  • 举报
回复

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "setBg(this);");
e.Row.Attributes.Add("onmouseover", "setOverBg(this);");
}
}


<script>
var lastItem;
var lastItemColor;
var lastSelectItem;
var lastSelectItemColr;
function setBg(obj)
{
if(lastSelectItem!=null){
lastSelectItem.style.backgroundColor=lastSelectItemColor;
}
lastSelectItem=obj;
lastSelectItemColor=lastItemColor;
obj.style.backgroundColor="#999999";
}
function setOverBg(obj)
{
if(lastItem!=null&&lastItem!=lastSelectItem){
lastItem.style.backgroundColor=lastItemColor;
}
lastItem=obj;
if(obj!=lastSelectItem){
lastItemColor=obj.style.backgroundColor;
obj.style.backgroundColor="#E7E7E7";
}
}</script>
tiger8000 2012-01-13
  • 打赏
  • 举报
回复
e.Row.Attributes.Add("onclick", "把背景颜色换了就是了啊");
LYH_OHYES

你说的没错,是能实现单击后,这行变为选中行,变为红色,但是,在没有单击其它行之前,鼠标离开后,这行又恢复了原来的颜色。

我要的是,在没有再单击其它行前,这些一直处于选中行状态,颜色一直是红色,不受 onmouseover 和 onmouseout 影响。,再一次谢过 LYH_OHYES 出手帮忙!!


现在完整代码如下:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
//onmouseover 鼠标悬停效果,变色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
//onmouseout 鼠标移除效果,恢复原来行的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
//单击后,让这行处于选中状态,变为红色
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#ff6633'");
//但是我想实现在没有单击其它行前,之前的那个选 中行,颜色要一直是红色的
//颜色不受 onmouseover 和 onmouseout 影响,直到有其它行被选 中,那时,这行
//才恢复原来颜色

}

}

LYH_OHYES 2012-01-13
  • 打赏
  • 举报
回复
e.Row.Attributes.Add("onclick", "把背景颜色换了就是了啊");
liuxin0121 2012-01-13
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='00BFFF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}

}
tiger8000 2012-01-13
  • 打赏
  • 举报
回复
LYH_OHYES

1楼,非常感谢你!!现在实现了一个功能,就是能实现 鼠标悬停效果,且鼠标移开后,整行恢复原来行的颜色

。但是有一个问题,就是单击选中这行后,如何能让这行变为红色,然后,在没有再单击其它行前,这些一直处于选中行状态,颜色一直是红色,不受 onmouseover 和 onmouseout 影响。这个要怎么实现呢

,再一次谢过 LYH_OHYES 出手帮忙!!


LYH_OHYES 2012-01-13
  • 打赏
  • 举报
回复

e.Row.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#66FF33'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");

再加个onclick事件变样式就行了

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧