How to change the gridview row colors in onclick event in javascript?

前端 未结 3 2033
执笔经年
执笔经年 2021-01-25 13:18

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

相关标签:
3条回答
  • 2021-01-25 13:56

    you can do like this...

     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 + "'" + ")");
        }       
    }
    

    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....

    0 讨论(0)
  • 2021-01-25 14:06
    in ur function use the row object to get the rows to loop over them and return them to there default color
    
      function ChangeRowColor(row) 
         {   
    
              var rows = row.parentNode.getElementsByTagName('TR');
              //loop over all rows and set there colors to default
              for(var i =0;i<rows.length;i++)
                {
                 rows[i].style.backgroundColor= 'White'; //if its your default color 
                }      
             //set the current row to be with the needed color
             row.style.backgroundColor = "YELLOW" //if this is the color needed onclick;
         }
    

    Regards

    0 讨论(0)
  • 2021-01-25 14:13

    You can call javascript function in GridView RowDataBound Event.

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
              {   
                 if (e.Row.RowType == DataControlRowType.DataRow)    
                      {  
                    e.Row.Attributes.Add("onClick", "ChangeColor('" + "GridView1','" + (e.Row.RowIndex+1).ToString() + "')");
                }
            }
    
    
    
    
        function ChangeColor(GridViewId, SelectedRowId) {
            var GridViewControl = document.getElementById(GridViewId);
            if (GridViewControl != null) {
                var GridViewRows = GridViewControl.rows;
                if (GridViewRows != null)
                {
                    var SelectedRow = GridViewRows[SelectedRowId];
                    //Remove Selected Row color if any
                    for (var i = 1; i < GridViewRows.length; i++) {
                        var row = GridViewRows[i];
                        if (row == SelectedRow) {
                            //Apply Yellow color to selected Row
                            row.style.backgroundColor = "#ffffda";
                        }
                        else {
                            //Apply White color to rest of rows
                            row.style.backgroundColor = "#ffffff";
                        }
                }
    
                }
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题