Results 1 to 2 of 2

Thread: Problem: ViewState

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Melbourne, Australia
    Posts
    12

    Exclamation Problem: ViewState

    I am trying to write a web control, but I've run into a major snag.

    I cannot find my ViewState data!

    Below is a sample of some code I wrote. It worked for a few hours.
    Code:
        Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
            If Not Me.Page.IsPostBack Then
                'First creation of control set default value
                ViewState.Add("IsSearch", True)
            End If
        End Sub
    
        Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
            If ViewState("IsSearch") Then
               'Do something
            Else
               'Do something else
            End If
        End Sub
    I initialise the ViewState with a value when it is first created, and the OnLoad event executes as desired (runs True section). However, when the page reloads, as postback event, the OnLoad event code fails (runs False section). The failure occurs because IsNothing(ViewState("IsSearch")) = True.

    Am I doing something wrong here or is Microsoft?

    -Drew

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Melbourne, Australia
    Posts
    12

    Smile

    Problem solved.

    I was doing something wrong.

    The cause was that the OnInit event occurs before the ViewState data is reloaded from the client.

    As soon as I re-located all code out of the OnInit event (to OnLoad) the problem disappeared.

    -Drew

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width