如何更改GridView的所选项目的背景颜色? [英] How do I change the gridview selected item background color?

查看:173
本文介绍了如何更改GridView的所选项目的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何改变Asp.net Web应用程序的GridView的所选项目背景颜色?

How do I change the gridview selected item background color in Asp.net web applications?

推荐答案

您可以尝试呼吁的onmouseover 事件的JavaScript函数。 <一href=\"http://www.c-sharpcorner.com/uploadfile/mahakgupta/how-to-change-color-of-rows-in-gridview-on-mouseover/\"相对=nofollow>这个网站有一个简单的例子:

You could try calling a javascript function on the onmouseover event. This website has a quick example:

在服务器端:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onmouseover"] =
            "javascript:mouseovercolor(this);";
        e.Row.Attributes["onmouseout"] =
            "javascript:mouseoutcolor(this);";
    }
}

在客户端:

<script language=javascript type="text/javascript">
    function mouseovercolor(mytxt) {
        mytxt.bgColor = 'Orange';
    }
    function mouseoutcolor(mytxt) {
        element.bgColor = 'White';
    }
</script>

编辑:这个网站有一个很好的例子就如何使之与的onClick 事件工作的:

Edited: This site has a nice example on how to make it work with the onClick event:

服务器端:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e){
  if (e.Row.RowType == DataControlRowType.DataRow){
    // javascript function to call on row-click event
    e.Row.Attributes.Add("onClick", "javascript:void SelectRow(this);");
  }
 }

客户端:

<script type="text/javascript">
         // format current row
         function SelectRow(row) {
             var _selectColor = "#303030";
             var _normalColor = "#909090";
             var _selectFontSize = "3em";
             var _normalFontSize = "2em";
             // get all data rows - siblings to current
             var _rows = row.parentNode.childNodes;
             // deselect all data rows
             try {
                 for (i = 0; i < _rows.length; i++) {
                     var _firstCell = _rows[i].getElementsByTagName("td")[0];
                     _firstCell.style.color = _normalColor;
                     _firstCell.style.fontSize = _normalFontSize;
                     _firstCell.style.fontWeight = "normal";
                 }
             }
             catch (e) { }
             // select current row (formatting applied to first cell)
             var _selectedRowFirstCell = row.getElementsByTagName("td")[0];
             _selectedRowFirstCell.style.color = _selectColor;
             _selectedRowFirstCell.style.fontSize = _selectFontSize;
             _selectedRowFirstCell.style.fontWeight = "bold";
         }
</script>

这篇关于如何更改GridView的所选项目的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆