I've been trying to change the color of each row in gridview1 based on a loop using JavaScript. However, whenever I click the Upload button, the loop runs but the color doesn't change as expected.
It's frustrating because I can't figure out what I'm missing.
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
With GridView1
For i = 1 To .Rows.Count - 1
btnUpload.Attributes.Add("onClick", "ChangeColor('" + "GridView1','" + (i).ToString() + "')")
Next
End With
End Sub
<script type="text/javascript">
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";
}
}
}
}
}
</script>