这是我编写的一个类,用于解决我在视图状态方面遇到的一些问题。它将信息存储在用户会话中并增加一个值来跟踪要显示的状态。困难在于不支持后退按钮。我最初这样做的原因是,在 AJAX 出现之前,视图状态被发送到客户端而不是存储在服务器上。我认为通过将其存储在服务器上,我可以使客户端体验更加丰富。现在,通过像速度这样的项目进行分布式缓存,可以将其存储在缓存中。
我很想听听人们可能有的一些评论和任何建议。
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
在您编写自己的代码以将视图状态保留到会话之前,您是否尝试过内置功能?
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 个状态。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)