-
Problem: ViewState
I am trying to write a web control, but I've run into a major snag.
I cannot find my ViewState data! :mad:
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? :confused:
-Drew
-
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