By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,526 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,526 IT Pros & Developers. It's quick & easy.

ViewState: Save & Load From database

P: n/a
Hello all!

I'm creating a web site in ASP.NET (VB.NET). One of the requirements was to
allow users to create orders going through several steps. A must have is to
have an option to save the work in any phase (any step) and to be able to
continue later.

My idea was to create several pages as steps (no, wizard control is not
suitable for several reasons) and to allow users to save any step to the
database, that is, a ViewState of the page (and page id) to the database.

I created a special Page class that overrides
LoadPageStateFromPersistenceMedium, SavePageStateToPersistenceMedium,
DeterminePostBackMode. Saving works great! The loading part sucks.

I have one textbox control for testing and cannot load text to it from saved
viewstate no matter what. Need help, big time.
Thanks!

Here's the code from my special Page class:

Imports Microsoft.VisualBasic

Public Class SaveStatePage
Inherits System.Web.UI.Page

Public MeSave As Boolean
Protected Overrides Function LoadPageStateFromPersistenceMedium() As
Object
If Me.Request.QueryString("IDsession") <> "" Then
Return Me.LoadSession()
Else
Return MyBase.LoadPageStateFromPersistenceMedium()
End If

End Function
Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal state As
Object)
If MeSave Then Me.SavePage(state)

MyBase.SavePageStateToPersistenceMedium(state)
End Sub

Protected Overrides Function DeterminePostBackMode() As
System.Collections.Specialized.NameValueCollection
If Me.Request.QueryString("IDsession") <> "" Then
Return Request.Form
Else
Return MyBase.DeterminePostBackMode()
End If
End Function

Protected Sub SavePage(ByVal state As Object)
Dim tmpLosFormatter As New LosFormatter
Dim tmpStream As New StringWriter

tmpLosFormatter.Serialize(tmpStream, state)

With New DataSetUitlityTableAdapters._viewstateTableAdapter
.InsertViewState(Session.SessionID, tmpStream.ToString, Now())
End With

End Sub

Protected Function LoadSession()
Dim tmpLosFormatter As New LosFormatter
Dim data As String

Dim tmpTable As DataSetUitlity._viewstateDataTable
Dim tmpReader As DataTableReader

data = " "
With New DataSetUitlityTableAdapters._viewstateTableAdapter
tmpTable =
..GetDataByIDsession(Me.Request.QueryString("IDses sion"))
End With
tmpReader = tmpTable.CreateDataReader()
If tmpReader.HasRows Then
tmpReader.Read()
Return
tmpLosFormatter.Deserialize(tmpReader.Item("ViewSt ate").ToString)
Else
Return tmpLosFormatter.Deserialize(data)
End If
End Function

Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

End Sub
End Class
May 18 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
contrary to popular belief, controls generally do not save their values in
viewstate. the browser posts the value, (and the value only). controls
generally store non value information it may need to process the postback
info.

for example the textbox needs to save the previous value, to implement the
onchange event, so the previous (render value) value is stored in
viewstate.it can then compare this to the postback value and tell if its
different.

generally to make you wizard work, you need to save the control values and
restore them, not the viewstate..

-- bruce (sqlwork.com)
"Irene" <Ir***@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
Hello all!

I'm creating a web site in ASP.NET (VB.NET). One of the requirements was
to
allow users to create orders going through several steps. A must have is
to
have an option to save the work in any phase (any step) and to be able to
continue later.

My idea was to create several pages as steps (no, wizard control is not
suitable for several reasons) and to allow users to save any step to the
database, that is, a ViewState of the page (and page id) to the database.

I created a special Page class that overrides
LoadPageStateFromPersistenceMedium, SavePageStateToPersistenceMedium,
DeterminePostBackMode. Saving works great! The loading part sucks.

I have one textbox control for testing and cannot load text to it from
saved
viewstate no matter what. Need help, big time.
Thanks!

Here's the code from my special Page class:

Imports Microsoft.VisualBasic

Public Class SaveStatePage
Inherits System.Web.UI.Page

Public MeSave As Boolean
Protected Overrides Function LoadPageStateFromPersistenceMedium() As
Object
If Me.Request.QueryString("IDsession") <> "" Then
Return Me.LoadSession()
Else
Return MyBase.LoadPageStateFromPersistenceMedium()
End If

End Function
Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal state As
Object)
If MeSave Then Me.SavePage(state)

MyBase.SavePageStateToPersistenceMedium(state)
End Sub

Protected Overrides Function DeterminePostBackMode() As
System.Collections.Specialized.NameValueCollection
If Me.Request.QueryString("IDsession") <> "" Then
Return Request.Form
Else
Return MyBase.DeterminePostBackMode()
End If
End Function

Protected Sub SavePage(ByVal state As Object)
Dim tmpLosFormatter As New LosFormatter
Dim tmpStream As New StringWriter

tmpLosFormatter.Serialize(tmpStream, state)

With New DataSetUitlityTableAdapters._viewstateTableAdapter
.InsertViewState(Session.SessionID, tmpStream.ToString, Now())
End With

End Sub

Protected Function LoadSession()
Dim tmpLosFormatter As New LosFormatter
Dim data As String

Dim tmpTable As DataSetUitlity._viewstateDataTable
Dim tmpReader As DataTableReader

data = " "
With New DataSetUitlityTableAdapters._viewstateTableAdapter
tmpTable =
.GetDataByIDsession(Me.Request.QueryString("IDsess ion"))
End With
tmpReader = tmpTable.CreateDataReader()
If tmpReader.HasRows Then
tmpReader.Read()
Return
tmpLosFormatter.Deserialize(tmpReader.Item("ViewSt ate").ToString)
Else
Return tmpLosFormatter.Deserialize(data)
End If
End Function

Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

End Sub
End Class

May 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.