ViewState: Save & Load From database

Discussion in 'ASP .Net' started by =?Utf-8?B?SXJlbmU=?=, May 18, 2006.

  1. 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("IDsession"))
    End With
    tmpReader = tmpTable.CreateDataReader()
    If tmpReader.HasRows Then
    tmpReader.Read()
    Return
    tmpLosFormatter.Deserialize(tmpReader.Item("ViewState").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
     
    =?Utf-8?B?SXJlbmU=?=, May 18, 2006
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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("IDsession"))
    > End With
    > tmpReader = tmpTable.CreateDataReader()
    > If tmpReader.HasRows Then
    > tmpReader.Read()
    > Return
    > tmpLosFormatter.Deserialize(tmpReader.Item("ViewState").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
    >
    >
     
    bruce barker \(sqlwork.com\), May 18, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. user
    Replies:
    1
    Views:
    784
    Bruce Barker
    Apr 4, 2005
  2. teo
    Replies:
    1
    Views:
    476
  3. sck10
    Replies:
    6
    Views:
    932
    sck10
    Sep 1, 2006
  4. Josema
    Replies:
    2
    Views:
    364
  5. Josema
    Replies:
    0
    Views:
    214
    Josema
    May 9, 2005
Loading...

Share This Page