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

前端 未结 3 2032
执笔经年
执笔经年 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 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";
                        }
                }
    
                }
            }
    
        }
    

提交回复
热议问题