All,
I'm trying to save the ViewState in a Session variable. Kind of like in this article:
One problem that I am having is with using RegisterHiddenField to set my HiddenInput "__ViewStateGuid".
It seems that when using RegisterHiddenField, once the field is there, I can't update the value of the field. Every post back will generate a new Guid and stuff the new viewstate string into the session. When the LoadPageStateFromPersistenceMedium event fires it grabs the value of the HiddenInput __ViewStateGuid and retrieves that session variable. The only problem is that __ViewStateGuid's value on the page was not changed the last time the SavePageStateToPersistenceMedium event fired.
Is there any way I can get __ViewStateGuid's value to update when using RegisterHiddenField? I've already tried writing the hidden field to the page by using Page.Controls.Add().
Here is the code:
* Sine scientia ars nihil est
* Respondeat superior
I'm trying to save the ViewState in a Session variable. Kind of like in this article:
One problem that I am having is with using RegisterHiddenField to set my HiddenInput "__ViewStateGuid".
It seems that when using RegisterHiddenField, once the field is there, I can't update the value of the field. Every post back will generate a new Guid and stuff the new viewstate string into the session. When the LoadPageStateFromPersistenceMedium event fires it grabs the value of the HiddenInput __ViewStateGuid and retrieves that session variable. The only problem is that __ViewStateGuid's value on the page was not changed the last time the SavePageStateToPersistenceMedium event fired.
Is there any way I can get __ViewStateGuid's value to update when using RegisterHiddenField? I've already tried writing the hidden field to the page by using Page.Controls.Add().
Here is the code:
Code:
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Text
Imports System.Web.UI
Namespace ViewStateHelper
Public Class PageState
Inherits Page
Private Const ViewStateHiddenFieldName As String = "__ViewStateGuid"
Protected Overloads Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
Dim los As LosFormatter = New LosFormatter
Dim writer As StringWriter = New StringWriter
los.Serialize(writer, viewState)
Dim vsGUID As String = Guid.NewGuid.ToString
Session(vsGUID.ToString) = writer.ToString
writer.Close()
[COLOR=green]
'Dim hd As New HtmlInputHidden
'hd.Attributes("id") = "__ViewStateGuid"
'hd.Attributes("name") = "__ViewStateGuid"
'hd.Value = vsGUID
'Page.Form.Controls.Add(hd) ** Generates Error "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)."
'Page.Controls.Add(hd) ** __ViewStateGuid is not in right place and the LoadPageState... function can't find it.
'vsGUID is not updated after inital page load. Causes wrong ViewState to be loaded
[/color] ClientScript.RegisterHiddenField(ViewStateHiddenFieldName, vsGUID)
End Sub
Protected Overloads Overrides Function LoadPageStateFromPersistenceMedium() As Object
If Not Session(Request.Form(ViewStateHiddenFieldName)) Is Nothing Then
Dim los As LosFormatter = New LosFormatter
Return los.Deserialize(Session(Request.Form(ViewStateHiddenFieldName)))
Else
Throw New Exception("The viewstate session " & Request.Form(ViewStateHiddenFieldName) & " is missing!")
End If
End Function
End Class
End Namespace
* Sine scientia ars nihil est
* Respondeat superior