如何保持页面上的GridView单元格背景颜色刷新

时间:2019-02-18 15:58:52

标签: javascript asp.net vb.net

我已经建立了一个asp.net gridview,当用户单击gridiview中的单元格时,它将经历一系列背景颜色更改。第一次单击时,单元格背景变成黄色,第二次单击时它变成橙色,在第三次单击时它变成蓝色,等等。但是,当我刷新页面时,背景色恢复为白色。当页面刷新或关闭并重新打开时,我需要单元格保持其背景色。

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

1 个答案:

答案 0 :(得分:0)

您可以使用浏览器本地存储来存储背景色:

有用的链接:

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

When is localStorage cleared?