GridView行颜色改变

在使用gridview绑定数据的时候,希望在鼠标移过行时,有点动态的变化。网络有专门的用户控件来实现,也有简单的方法实现 。
下面一种是非常简单的方法。在gridview的RowDataBound事件中添加代码即可:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //加入鼠标滑过的高亮效果
        if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
        {
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;
this.style.backgroundColor='inactivecaptiontext',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色
            e.Row.Attributes.Add("onmouseout",
 "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
         }
    }

另外也可以把这段代码放入webservice中,作为公共代码使用,改写后为:
webservice.cs文件中定义
public void mouseColor(GridViewRowEventArgs e)
    {
        //加入鼠标滑过的高亮效果
        if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
        {
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;
this.style.backgroundColor='inactivecaptiontext',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色
            e.Row.Attributes.Add("onmouseout", 
"this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
         }
    }

在需要使用的地方使用该代码的方法为 :
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        WebService webs = new WebService();
        webs.mouseColor(e);
    }

posted @ 2009-03-04 23:55  YaSin  阅读(1781)  评论(0)    收藏  举报