Class That use ViewState/session

M

Max

I need an vb.net class that is invoked from aspx page, that use the
viewstate/session object.
This class must be store the information into viewstate/session.

Can you give me an example ?
Thanks
 
T

Teemu Keiski

You can do it so that Page calls a method of the class, which returns
something which is again placed into ViewState or Session by the Page. This
way class doesn't need to know about the ViewState collection like the key
used and so on, which is actually better for many reasons

-no chance for name collisions
-class library is usable from other type projects than web projects (Windows
Forms, or WinServices...)
-class does only it's job, no extra reponsibilities
-clear workings for the class consumer

'Code on the Page
Dim cIns As New TheClassInstance()
ViewState("data") = cIns.returnSomething()

'Then again getting it back
....
 
U

Usenet User

I need an vb.net class that is invoked from aspx page, that use the
viewstate/session object.
This class must be store the information into viewstate/session.

Can you give me an example ?
Thanks

The easiest way would be to pass the ViewState from you Page class.
Here is an example (sorry if the syntax is a bit wrong, I am not
really a VB guy):


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
....
 
M

Max

hi,
from aspx page, i instance an class.
in an method of this class, i need insert into viewstate a data.
can i reference from class to viewstate of the page that have instance the
class ?
Thanks
 
T

Teemu Keiski

Hi,

As is described on following reply, yes you can, but it's not that good
approach. However, if you just want it done and don't care about what else
it might mean, then sure you can just pass the ViewState collection from the
Page to the class and use it (just pay attention to the drawbacks I
mentioned)
 
M

Max

thanks,
but it necessary to pass the viewstate reference from aspx page to class ?
it's no possibile that class use viewstate without pass it from aspx page ?
thanks
 
T

Teemu Keiski

If your class would be a control, it would have its own ViewState collection
(which would be saved and restored by the page framework).

However, if your class is stand-alone custom class, it either can take the
ViewState collection as a reference in and operate with 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.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top