想法如何使用ASP.NET AJAX和更新面板时处理视图状态 [英] Ideas for how to deal with viewstate when using ASP.NET AJAX and update panels

查看:117
本文介绍了想法如何使用ASP.NET AJAX和更新面板时处理视图状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我写的,以解决我与ViewState中有一些问题的一类。它存储在用户会话中的信息,并增加一个值来跟踪要显示的状态。困难的是,没有为返回键的支持。我本来这样做的原因是,以前AJAX沿着视图状态来到当时正在发送到客户端,而不是存储在服务器上。我想,通过将其存储在服务器上,我可以让客户体验更丰富。现在通过像速度的项目的分布式缓存这可以存储在一个高速缓存中。

我很想听到一些意见和任何提示人们可能。

 公共类的ViewState
    '''<总结>
    '''保存状态到用户会话
    '''< /总结>
    '''< PARAM NAME =视图状态>该视图状态对象序列化< /参数>
    '''< PARAM NAME =NoSerialize>< /参数>
    '''<退货和GT;返回所保存的字符串的KEY< /回报>
    '''<&言论GT;< /言论>
    功能即时存档(BYVAL视图状态为对象,可选BYVAL NoSerialize由于布尔= FALSE)
        HttpContext.Current.Session(VSID)= HttpContext.Current.Session(VSID)+1
        如果NoSerialize然后
            HttpContext.Current.Session(CacheString)=的ViewState
        其他
            昏暗格式作为新LosFormatter
            昏暗的作家作为新System.IO.StringWriter
            Format.Serialize(作家,ViewState中)
            HttpContext.Current.Session(CacheString)= Writer.ToString
        万一
        返回HttpContext.Current.Session(VSID)
    结束功能
    '''<总结>
    '''返回视图状态对象的序列化副本
    '''< /总结>
    '''< PARAM NAME =SQLSTATE>< /参数>
    '''< PARAM NAME =NoSerialize>< /参数>
    '''<&回报GT;< /回报>
    '''<&言论GT;< /言论>
    功能LoadState的(BYVAL SQLSTATE,可选BYVAL NoSerialize = FALSE)作为对象
        如果NoSerialize然后
            如果不HttpContext.Current.Session(CacheString(SQLSTATE))是没有那么
                返回HttpContext.Current.Session(CacheString(SQLSTATE))
            其他
                HttpContext.Current.Trace.Warn(没有ViewState对象中找到)
            万一
        其他
            昏暗格式作为新LosFormatter
            如果不HttpContext.Current.Session(CacheString(SQLSTATE))是没有那么
                返回Format.Deserialize(HttpContext.Current.Session(CacheString(SQLSTATE)))
            其他
                HttpContext.Current.Trace.Warn(没有ViewState对象中找到)
            万一
        万一
    结束功能
    '''<总结>
    '''AJAX视图状态
    '''保存状态到用户会话
    '''< /总结>
    '''< PARAM NAME =视图状态>该视图状态对象序列化< /参数>
    '''< PARAM NAME =VSID>该页面用于查找视图状态的项目&lt的ID; /参数>
    '''< PARAM NAME =NoSerialize>< /参数>
    '''<退货和GT;返回所保存的字符串的KEY< /回报>
    '''<&言论GT;< /言论>
    功能即时存档(BYVAL视图状态作为对象,BYVAL VSID作为字符串,可选BYVAL NoSerialize由于布尔= FALSE)
        HttpContext.Current.Session(VSID)= HttpContext.Current.Session(VSID)+1
        如果NoSerialize然后
            如果VSID =那
                HttpContext.Current.Session(CacheString)=的ViewState
                返回HttpContext.Current.Session(VSID)
            其他
                HttpContext.Current.Session(VSID)= HttpContext.Current.Session(VSID)+1
                HttpContext.Current.Session(CacheString)=的ViewState
                HttpContext.Current.Session(CacheString(VSID))=的ViewState
                返回HttpContext.Current.Session(VSID)
            万一
        其他
            昏暗格式作为新LosFormatter
            昏暗的作家作为新System.IO.StringWriter
            Format.Serialize(作家,ViewState中)
            如果VSID =那
                HttpContext.Current.Session(VSID)= HttpContext.Current.Session(VSID)+1
                HttpContext.Current.Session(CacheString)= Writer.ToString
                返回HttpContext.Current.Session(VSID)
            其他
                HttpContext.Current.Session(VSID)= HttpContext.Current.Session(VSID)+1
                HttpContext.Current.Session(CacheString)= Writer.ToString
                HttpContext.Current.Session(CacheString(VSID))= Writer.ToString
                返回HttpContext.Current.Session(VSID)
            万一
        万一
    结束功能
    '''<总结>
    '''获取字符串重新presenting缓存的视图状态对象。
    '''< /总结>
    '''< PARAM NAME =VSID>的覆盖会话VSID属性,并使用你自己的< /参数>
    '''< VALUE>< /值>
    '''<&回报GT;< /回报>
    '''<&言论GT;< /言论>
    公共只读属性CacheString(可选BYVAL VSID为整数= 0)作为字符串
        得到
            如果VSID = 0则
                返回的ViewState&放大器; HttpContext.Current.Session(VSID)
            其他
                返回的ViewState&放大器; VSID
            万一
        到底得的
    高端物业
末级

以下code进去背后为你的asp.net页面code:

 受保护的覆盖子SavePageStateToPersistenceMedium(BYVAL ViewState中作为对象)
        昏暗VState作为新的ViewState
        如果String.IsNullOrEmpty(的Request.Form(__ SQLVIEWSTATE))= false,那么
            RegisterHiddenField(__ SQLVIEWSTATE,VState.SaveState(视图状态,的Request.Form(__ SQLVIEWSTATE)))
        其他
            RegisterHiddenField(__ SQLVIEWSTATE,VState.SaveState(视图状态))
        万一
    结束小组
    受保护的覆盖功能的LoadPageStateFromPersistenceMedium()作为对象
        昏暗VState作为新的ViewState
        返回VState.LoadState(的Request.Form(__ SQLVIEWSTATE))
    结束功能


解决方案

在你滚你自己的code坚持视图状态的会话,你尝试过内置的功能?

 保护覆盖PageStatePersister PageStatePersister
{
    得到
    {
        返回新SessionPageStatePersister(本);
    }
}

(如图C#版本)。也许这将解决您的后退按钮的问题?

修改回答评论:我觉得这是VB等价的:

 公共覆盖功能GetStatePersister()作为PageStatePersister
  返回新SessionPageStatePersister(页)
端功能GetStatePersister

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

Here is a class that I wrote to solve some issues that I was having with viewstate. It stores the info in the users session and increments a value to track which state to show. The difficulty is that there is no support for the back button. The reason I originally had this was that before AJAX came along the viewstate was being send down to the client instead of being stored on the server. I figured that by storing it on the server i could make the client experience much richer. Now with distributed caching through projects like velocity this could be stored in a cache.

I'd love to hear some comments and any tips that people might have.

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

The following code goes in the code behind for your asp.net page:

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

解决方案

Before you roll your own code to persist the viewstate to the session, have you tried the built in functionality?

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

(C# version shown). Maybe this will solve your back button issue?

Edit to answer comment: I think this is the VB equivalent:

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

I don't know how this handles the back-button, but I believe that it stores up to 10 states per page.

这篇关于想法如何使用ASP.NET AJAX和更新面板时处理视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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