The easiest way would be to pass the ViewState from you Page class.
Here is an example:
In Page code:
….
‘Create an instance of custom class
Dim objMyClass As MyClass
Set objMyClass = New MyClass( Me.ViewState )
….
In class code:
….
Private viewState As System.Web.UI.StateBag
Public New( ByRef pageViewState As System.Web.UI.StateBag )
Set viewState = pageViewState
End
….
However, if your class is stand-alone custom class, it either can take the ViewState collection as a reference in and operate ith it, or it can just return the value(s) to the caller (Page) and let that handle operating with the ViewState (there are examples of both in this thread).
Only controls (Page and its controls) have ViewState collection (each control has its own) and therefore accessing it from a custom class needs certain approach. Sessions you could access from class via System.Web.HttpContext.Current.Session, but there’s not similar way to access ViewState.
ReferenceÂ
Â