回发后视图状态不会持续存在 [英] Viewstate does not persist after postback

查看:29
本文介绍了回发后视图状态不会持续存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为控件 (vb.net) 的回发时保留视图状态时遇到了一些问题

I'm having some problems in persisting the viewstate on postback for a Control (vb.net)

这是我控制的一些代码:

here's some code i've put in my control:

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
            MyBase.OnInit(e)
            Me.EnableViewState = True
            Me.ViewStateMode = System.Web.UI.ViewStateMode.Enabled
            If Not Page.IsPostBack Then
                _SortTime = DateTime.Now
                _SortTime.AddSeconds(-10) ' incase the fileserver and webserver date are out of sync
                ViewState("PageLoadTimeStamp") = _SortTime      
            End If
        End Sub

加载:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        _SortTime = CType(ViewState("PageLoadTimeStamp"), DateTime)

End Sub

但是在每次回发时,viewstate 都会再次为空.

But on every postback, the viewstate is empty again.

更新:我想做什么?我想存储用户第一次加载页面的时间.然后,用户将进行多次回发.在每次回传时,我都需要知道那个时间.

Update: What do i want to do? I want to store the time when the user first loads the page. Then, the user will do multiple postbacks. On every postback i need to know that time.

更新 2: 上下文:

  • aspx 页面 (Editor.aspx) 将动态加载 Web 控件 (ObjectsEditor.ascx)
  • ObjectsEditor.ascx 包含单个自定义控件 (Objects.vb)
  • Objects.vb 将创建另一个自定义控件 (ObjectsContainer.vb) 并将其添加到它的控件中需要持久化视图状态的是 ObjectsContainer.

我在任何地方都设置了 EnableViewState = "true",但仍然没有结果.我真的需要在这里设置这些属性吗?(ViewStateMode 和 EnableViewState)如果没有,我应该在哪里处理这个问题,这些属性之间有什么区别?

I'm setting EnableViewState = "true" everywhere i can, but still no result. Do i really need to set those properties here. (ViewStateMode and EnableViewState) If not, where should i handle this and what is the difference between those properties?

非常感谢.

推荐答案

我无法找到回发后视图状态没有持续存在的原因,但我找到了另一种使用 controlstate 保存值的方法.这是我的实现:

I can't find why the viewstate did not persist after postback, but I've found another way to hold the value using the controlstate. Here's my implementation:

 Public Property SortTime() As DateTime
        Get
            Return _SortTime
        End Get
        Set(ByVal value As DateTime)
            _SortTime = value
        End Set
    End Property

添加以下覆盖方法:

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        Page.RegisterRequiresControlState(Me)
        MyBase.OnInit(e)
    End Sub

    Protected Overrides Function SaveControlState() As Object
        Return _SortTime
    End Function

    Protected Overrides Sub LoadControlState(ByVal savedState As Object)
        Dim state As DateTime = CType(savedState, DateTime)
        Me._SortTime = state
    End Sub

为 OnLoad 中的属性赋值

Give a value to the property in OnLoad

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            SortTime = DateTime.Now
        End If
    End Sub

现在我可以在页面加载后调用该属性,并且它将始终在第一次加载页面时设置该值.它不会在回发时改变,也不会在回发时消失.

Now i can call the property after the page has loaded and it will always have the value set on the first page load. It will not change on postback and it won't be gone on postback.

我确信这对于 viewstate 也是可能的,但由于某种原因,这对我不起作用.

I'm sure this is possible with the viewstate too, but for some reason, this was not working for me.

感谢您帮助我了解 Viewstate,我相信我迟早要使用 viewstate.

Thanks for helping me to understand the Viewstate, I'm sure i'll have to use the viewstate sooner or later.

这篇关于回发后视图状态不会持续存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆