0

I have set up an asp.net gridview, and when a user clicks on a cell in the gridiview it goes through a series of background color changes. On the first click the cell background turns yellow, on the second click it turns orange, on the third click it turns blue, etc. However, when I refresh the page, the background color reverts to white. I need the cells to keep their background color when the page is refreshed or closed and re-opened.

VB:

Private Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowCreated
    'Changes background color on click (JavaScript)
    For x As Byte = 1 To 13
        e.Row.Cells(x).Attributes.Add("onclick", "toggle(this);")
    Next
End Sub

JS:

function toggle(obj){
if (obj.style.backgroundColor == 'white') {
    obj.style.backgroundColor = 'yellow';
} else if (obj.style.backgroundColor == 'yellow') {
    obj.style.backgroundColor = 'orange';
} else if (obj.style.backgroundColor == 'orange') {
    obj.style.backgroundColor = 'deepskyblue';
} else if (obj.style.backgroundColor == 'deepskyblue') {
    obj.style.backgroundColor = 'lightgreen';
} else if (obj.style.backgroundColor == 'lightgreen') {
    obj.style.backgroundColor = 'white';
} else {
    obj.style.backgroundColor = 'white';
}}
Taylor
  • 1
  • 1
  • It might end up being a bit more complicated than you would hope. You'll have to send this information somehow to the server. Could be through ajax or a hidden input field. The data should contain the cell location and the type of color. – the_lotus Feb 18 '19 at 16:04

1 Answers1

0

You can use browser localstorage for storing background color:

Helpful links:

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

When is localStorage cleared?

Mohsin Mehmood
  • 4,156
  • 2
  • 12
  • 18