if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.Attributes.Add("onclick", "this.style.backgroundColor=this.style.backgroundColor=='#000000'?'#ebebce':'#000000';");
}
这段代码问了不少地方 代码本身没问题
点击第一次是正常的 不知道为什么 点击第二次的时候没反映
求高手帮忙看下 用的VS2010 控件GridView显示 事件RowDataBound
(怨念啊!今天弄这东西弄了一整天了!)

解决方案 »

  1.   

    方法一、  JS代码:<script type="text/javascript">var lastColor = null;
    var lastRow = null;function RowSelected(rowID)
    {
        var row = document.getElementById(rowID);
        if(lastRow == null)
        {
           lastColor = row.style.backgroundColor;
           lastRow = row;
           row.style.backgroundColor = 'PeachPuff';
        }
        else
        {
           if(lastRow == row) return;
           lastRow.style.backgroundColor = lastColor;
           lastColor = row.style.backgroundColor;
           lastRow = row;
           row.style.backgroundColor = 'PeachPuff';
        }
    }</script>  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                   e.Row.Attributes.Add("onclick", "RowSelected('" + e.Row.ClientID + "')");//点击行变色
                               }    
            } 
        } 方法二  JS代码:<script type="text/javascript">  var prevselitem=null;
          function selectx(row)
         {
                 if(prevselitem!=null)
                 {
                     prevselitem.style.backgroundColor='#ffffff';
                 }
                 row.style.backgroundColor='PeachPuff';
                 prevselitem=row;
                
          }</script>  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)";);//点击行变色
                            } 
        }
      

  2.   

    那是因为你的判断条件是没有满足。你可以这样  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
        if (e.Row.RowType == DataControlRowType.DataRow){
          e.Row.Attributes.Add("onclick", "myfunction(this);");
        }
      }<script type="text/javascript">
      var color = ["#000000", "#ebebce"];
      function myfunction(o) {
        i = parseInt(o.getAttribute("click"));
        if (isNaN(i)) i = 1;
        o.style.backgroundColor = color[i % 2];
        o.setAttribute("click", ++i);
      }
    </script>