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
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";
}
}
}
}
}