如何改变在GridView行的颜色在onclick事件在JavaScript? [英] How to change the gridview row colors in onclick event in javascript?

查看:148
本文介绍了如何改变在GridView行的颜色在onclick事件在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在ASP.NET以下 GridView控件 code。我设置了 AlternatingRow 风格的背景色来浓汤。其余的行设置为白色。

I wrote the following GridView code in ASP.NET. I set the AlternatingRow style's BackColor to bisque. The remaining rows are set to white.

这code我的 grdRequests_RowDataBound 事件中是否存在:

This code exists within my grdRequests_RowDataBound event:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    e.Row.Attributes.Add("onclick", "ChangeRowColor(this)");
    e.Row.Attributes.Add("onmouseover", "this.style.cursor=\'pointer\'");
}

中的JavaScript ChangeRowColor code以上如下:

function ChangeRowColor(row) 
{
    if (previousRow == row) 
        return;         

    else if (previousRow != null)
        var color = row.style.backgroundColor;

    if (previousRow != null) {

        alert(color)

        if (color == "bisque") {
            previousRow.style.backgroundColor = "white";
        }
        else if (color == "white") {
            previousRow.style.backgroundColor = "bisque";
        }        
    }

    row.style.backgroundColor = "#ffffda";
    previousRow = row;    
}

当我点击该行,我需要改变喜欢黄色的颜色。在选择另一行后,我需要切换previous行的颜色回到其旧的颜色,但在我的code这不起作用。有什么建议?

When I click the row, I need to change the color like yellow. After selecting another row, I need to switch the previous row's color back to its old color, but in my code this doesn't work. Any suggestions?

推荐答案

您可以像这样...

 protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
 {

    string rowStyle = "this.style.backgroundColor
    = 'yellow'";
    string rowStyleClickedTwice =
    "this.style.backgroundColor = 'blue'";
    string rowID = String.Empty; 

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        rowID = "row"+e.Row.RowIndex; 

        e.Row.Attributes.Add("id",
        "row"+e.Row.RowIndex);
        e.Row.Attributes.Add("onclick",
        "ChangeRowColor(" +"'" + rowID + "'" + ")");
    }       
}

这是Java脚本code:

And this is the Java Script code:

 <input type="hidden" id="hiddenColor"  />
 <script language ="javascript" type="text/javascript">

  document.body.style.cursor = 'pointer'; 


 function ChangeRowColor(rowID) 
 { 
     var color = document.getElementById(rowID).style.backgroundColor;
     alert(color);   

     if(color != 'yellow') 
     document.getElementById("hiddenColor").style.backgroundColor = color;

     alert(oldColor); 

     if(color == 'yellow')
    document.getElementById(rowID).style.backgroundColor = document.getElementById("hiddenColor").style.backgroundColor;
     else
     document.getElementById(rowID).style.backgroundColor = 'yellow';             

  }
</script>

我希望它会帮助你......

i hope it will helps you....

这篇关于如何改变在GridView行的颜色在onclick事件在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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