【问题标题】:How to keep GridView cell background color on page refresh如何在页面刷新时保持 GridView 单元格背景颜色
【发布时间】:2019-02-18 15:58:52
【问题描述】:

我已经设置了一个 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';
}}

【问题讨论】:

  • 它最终可能比您希望的要复杂一些。您必须以某种方式将此信息发送到服务器。可以通过 ajax 或隐藏的输入字段。数据应包含单元格位置和颜色类型。

标签: javascript asp.net vb.net


【解决方案1】:

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

有用的链接:

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

When is localStorage cleared?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2016-02-26
    相关资源
    最近更新 更多