【问题标题】:Ideas for how to deal with viewstate when using ASP.NET AJAX and update panels使用 ASP.NET AJAX 和更新面板时如何处理视图状态的想法
【发布时间】:2010-09-21 05:44:40
【问题描述】:

这是我编写的一个类,用于解决我在视图状态方面遇到的一些问题。它将信息存储在用户会话中并增加一个值以跟踪要显示的状态。困难在于不支持后退按钮。我最初有这个的原因是在 AJAX 出现之前,视图状态被发送到客户端而不是存储在服务器上。我认为通过将其存储在服务器上,我可以使客户体验更加丰富。现在通过速度等项目的分布式缓存,可以将其存储在缓存中。

我很想听听一些 cmets 和人们可能有的任何提示。

Public Class ViewState
    ''' <summary>
    ''' Saves the state to the users session
    ''' </summary>
    ''' <param name="viewState">The viewstate object to serialize</param>
    ''' <param name="NoSerialize"></param>
    ''' <returns>Returns the KEY of the string that was saved.</returns>
    ''' <remarks></remarks>
    Function SaveState(ByVal viewState As Object, Optional ByVal NoSerialize As Boolean = False)
        HttpContext.Current.Session("VSID") = HttpContext.Current.Session("VSID") + 1
        If NoSerialize Then
            HttpContext.Current.Session(CacheString) = viewState
        Else
            Dim Format As New LosFormatter
            Dim Writer As New System.IO.StringWriter
            Format.Serialize(Writer, viewState)
            HttpContext.Current.Session(CacheString) = Writer.ToString
        End If
        Return HttpContext.Current.Session("VSID")
    End Function
    ''' <summary>
    ''' Returns a deserialized copy of the viewstate object
    ''' </summary>
    ''' <param name="SQLSTATE"></param>
    ''' <param name="NoSerialize"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function LoadState(ByVal SQLSTATE, Optional ByVal NoSerialize = False) As Object
        If NoSerialize Then
            If Not HttpContext.Current.Session(CacheString(SQLSTATE)) Is Nothing Then
                Return HttpContext.Current.Session(CacheString(SQLSTATE))
            Else
                HttpContext.Current.Trace.Warn("No ViewState Object Found")
            End If
        Else
            Dim Format As New LosFormatter
            If Not HttpContext.Current.Session(CacheString(SQLSTATE)) Is Nothing Then
                Return Format.Deserialize(HttpContext.Current.Session(CacheString(SQLSTATE)))
            Else
                HttpContext.Current.Trace.Warn("No ViewState Object Found")
            End If
        End If
    End Function
    ''' <summary>
    ''' AJAX Viewstate
    ''' Saves the state to the users session
    ''' </summary>
    ''' <param name="viewState">The viewstate object to serialize</param>
    ''' <param name="VSID">The ID that the page uses to find the viewstate item</param>
    ''' <param name="NoSerialize"></param>
    ''' <returns>Returns the KEY of the string that was saved.</returns>
    ''' <remarks></remarks>
    Function SaveState(ByVal viewState As Object, ByVal VSID As String, Optional ByVal NoSerialize As Boolean = False)
        HttpContext.Current.Session("VSID") = HttpContext.Current.Session("VSID") + 1
        If NoSerialize Then
            If VSID = "" Then
                HttpContext.Current.Session(CacheString) = viewState
                Return HttpContext.Current.Session("VSID")
            Else
                HttpContext.Current.Session("VSID") = HttpContext.Current.Session("VSID") + 1
                HttpContext.Current.Session(CacheString) = viewState
                HttpContext.Current.Session(CacheString(VSID)) = viewState
                Return HttpContext.Current.Session("VSID")
            End If
        Else
            Dim Format As New LosFormatter
            Dim Writer As New System.IO.StringWriter
            Format.Serialize(Writer, viewState)
            If VSID = "" Then
                HttpContext.Current.Session("VSID") = HttpContext.Current.Session("VSID") + 1
                HttpContext.Current.Session(CacheString) = Writer.ToString
                Return HttpContext.Current.Session("VSID")
            Else
                HttpContext.Current.Session("VSID") = HttpContext.Current.Session("VSID") + 1
                HttpContext.Current.Session(CacheString) = Writer.ToString
                HttpContext.Current.Session(CacheString(VSID)) = Writer.ToString
                Return HttpContext.Current.Session("VSID")
            End If
        End If
    End Function
    ''' <summary>
    ''' Gets the string representing the cached viewstate object.
    ''' </summary>
    ''' <param name="VSID">Use this to override the session VSID property and use your own.</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property CacheString(Optional ByVal VSID As Integer = 0) As String
        Get
            If VSID = 0 Then
                Return "ViewState" & HttpContext.Current.Session("VSID")
            Else
                Return "ViewState" & VSID
            End If
        End Get
    End Property
End Class

以下代码位于您的 asp.net 页面背后的代码中:

Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
        Dim VState As New ViewState
        If String.IsNullOrEmpty(Request.Form("__SQLVIEWSTATE")) = False Then
            RegisterHiddenField("__SQLVIEWSTATE", VState.SaveState(viewState, Request.Form("__SQLVIEWSTATE")))
        Else
            RegisterHiddenField("__SQLVIEWSTATE", VState.SaveState(viewState))
        End If
    End Sub
    Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
        Dim VState As New ViewState
        Return VState.LoadState(Request.Form("__SQLVIEWSTATE"))
    End Function

【问题讨论】:

    标签: asp.net vb.net asp.net-ajax viewstate


    【解决方案1】:

    在您使用自己的代码将视图状态持久保存到会话之前,您是否尝试过内置功能?

    protected override PageStatePersister PageStatePersister
    {
        get
        {
            return new SessionPageStatePersister(this);
        }
    }
    

    (显示 C# 版本)。也许这可以解决您的后退按钮问题?

    编辑回答评论:我认为这是 VB 的等价物:

    Public Overrides Function GetStatePersister() As PageStatePersister
      Return New SessionPageStatePersister(Page)
    End Function 'GetStatePersister
    

    我不知道它如何处理后退按钮,但我相信它每页最多可存储 10 个状态。

    【讨论】:

    • 是否有 VB.net 版本,当页面上有更新面板时,它究竟如何处理回发和视图状态?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多