注册 登录
编程论坛 ASP.NET技术论坛

如何实现鼠标指向GridView控件的行时,该行改变颜色,离开该行时又恢复原来的颜色?

foshan 发布于 2007-03-08 17:51, 1564 次点击
如何实现鼠标指向GridView的行时,该行改变颜色,离开该行时又恢复原来的颜色???这个肯定要用到JavaScript脚本了吗?还是GridView控件本身有此功能???
15 回复
#2
mldark2007-03-08 17:58
这个啊在属性里面应该有的 你找找吧


#3
foshan2007-03-09 08:30
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == (DataControlRowState.Selected | DataControlRowState.Normal)) && e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#B1CFF8'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E3EAEB';");
}
if ((e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == (DataControlRowState.Selected | DataControlRowState.Alternate))
&& e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#B1CFF8'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White';");
}
}

测试通过,但目前还未完全看明白代码的意思。
#4
foshan2007-03-09 08:35
还有一个问题,我的一个页面中有五个GridView控件,均要求要实现鼠标指向GridView的行时,该行改变颜色,离开该行时又恢复原来的颜色。如果逐个GridView控件均在RowCreated事件加上上述代码,变得代码太繁琐,重复利用率不高。如何能实现五个GridView控件共享上述的代码???谢谢!
#5
foshan2007-03-09 08:48
这样好像可以,大家看看。

protected void GridView5_RowCreated(object sender, GridViewRowEventArgs e)
{
Change(e);
}
protected void GridView4_RowCreated(object sender, GridViewRowEventArgs e)
{
Change(e);
}
protected void GridView3_RowCreated(object sender, GridViewRowEventArgs e)
{
Change(e);
}
protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
{
Change(e);
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
Change(e);
}
protected void Change(GridViewRowEventArgs e)
{
if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == (DataControlRowState.Selected | DataControlRowState.Normal)) && e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#B1CFF8'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E3EAEB';");
}
if ((e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == (DataControlRowState.Selected | DataControlRowState.Alternate))
&& e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#B1CFF8'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White';");
}
}
#6
cyyu_ryh2007-03-09 12:36
就是个鼠标事件
#7
lq73506842007-03-09 16:02
我会asp里实现这个功能的代码.
<tr onMouseOver="this.bgColor='#d2d2d2'" onMouseOut="this.bgColor='#f8f8f8'" bgcolor="#f8f8f8">

我不会.net,现在想学,今天第一次来本区,不知道这个asp里的代码能用在.net里不.或者是不是可以改一下.
望高手指点.
#8
foshan2007-05-23 15:17
还有一个问题:在此基础上,如何实现对于 已选定 的行,光标的移过 已选定的行 不改变其颜色,只改变非已选定行的颜色?谢谢!
#9
foshan2007-05-24 09:33
去掉上边代码中的selected状态(如下代码),遇到的问题是:当选定一行后,然后上下移动鼠标,该选定行的背景颜色就变成了不是选定行的背景颜色了,然后再选定该行,然后再上下移动鼠标,这时该选定行的背景颜色就不会改变了。
换言之,就是要连续点击 两次 某行(连续两次选定该行),才会使该选定行的背景颜色不会因鼠标的onmouseover和onmouseout事件而改变。
怎么样才能实现 一次 选定该行后,上下移动鼠标,不会改变该选定行的背景颜色???谢谢!

protected void Change(GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Normal && e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#B1CFF8'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E3EAEB';");
}
if (e.Row.RowState == DataControlRowState.Alternate && e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#B1CFF8'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White';");
}
}

只有本站会员才能查看附件,请 登录

[此贴子已经被作者于2007-5-24 9:38:11编辑过]

#10
foshan2007-05-27 11:04
#11
rainic2007-05-28 09:20
以下是引用lq7350684在2007-3-9 16:02:19的发言:
我会asp里实现这个功能的代码.
<tr onMouseOver="this.bgColor='#d2d2d2'" onMouseOut="this.bgColor='#f8f8f8'" bgcolor="#f8f8f8">

我不会.net,现在想学,今天第一次来本区,不知道这个asp里的代码能用在.net里不.或者是不是可以改一下.
望高手指点.

当然"不能"...
不过到最后返回客户端的Html是差不多的

#12
GrimFish2007-05-28 09:38
才5个就烦琐了吗?一个页面用5个GRIDVIEW我真的绝对太烦了。不知道你用来干什么

写个用户控件不就可以完成所有的了。
#13
foshan2007-05-31 17:33

第9楼的问题还未解决,继续求助,谢谢大家!

#14
Arui2007-05-31 19:36
protected void GridView1_RowDataBound(object sender,GridViewEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#123456'");
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c");
}

}

OK!
#15
foshan2007-06-01 08:25
以下是引用Arui在2007-5-31 19:36:15的发言:
protected void GridView1_RowDataBound(object sender,GridViewEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#123456'");
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c");
}

}

OK!

谢谢!但未能解决第9楼碰到的问题:当选定一行后,然后上下移动鼠标,该选定行的背景颜色就变成了不是选定行的背景颜色了……
请大家继续帮忙,谢谢大家!

[此贴子已经被作者于2007-6-1 8:26:50编辑过]

#16
zd1234562007-06-01 17:13

发个贴 下次 好找到

1