By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

Class That use ViewState/session

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
....

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

"Max" <ma*******@hotmail.com> wrote in message
news:3a*************@individual.net...
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

Nov 21 '05 #2

P: n/a
On Thu, 24 Mar 2005 19:19:12 +0100, "Max" <ma*******@hotmail.com>
wrote:
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
....
Nov 21 '05 #3

P: n/a
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
"Teemu Keiski" <jo****@aspalliance.com> ha scritto nel messaggio
news:OG**************@TK2MSFTNGP12.phx.gbl...
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
...

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

"Max" <ma*******@hotmail.com> wrote in message
news:3a*************@individual.net...
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


Nov 21 '05 #4

P: n/a
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)

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

"Max" <ma*******@hotmail.com> wrote in message
news:3a*************@individual.net...
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
"Teemu Keiski" <jo****@aspalliance.com> ha scritto nel messaggio
news:OG**************@TK2MSFTNGP12.phx.gbl...
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
...

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

"Max" <ma*******@hotmail.com> wrote in message
news:3a*************@individual.net...
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



Nov 21 '05 #5

P: n/a
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

"Teemu Keiski" <jo****@aspalliance.com> ha scritto nel messaggio
news:%2****************@TK2MSFTNGP10.phx.gbl...
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)

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

"Max" <ma*******@hotmail.com> wrote in message
news:3a*************@individual.net...
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
"Teemu Keiski" <jo****@aspalliance.com> ha scritto nel messaggio
news:OG**************@TK2MSFTNGP12.phx.gbl...
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
...

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

"Max" <ma*******@hotmail.com> wrote in message
news:3a*************@individual.net...
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



Nov 21 '05 #6

P: n/a
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

"Max" <ma*******@hotmail.com> wrote in message
news:3a*************@individual.net...
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

"Teemu Keiski" <jo****@aspalliance.com> ha scritto nel messaggio
news:%2****************@TK2MSFTNGP10.phx.gbl...
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)

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

"Max" <ma*******@hotmail.com> wrote in message
news:3a*************@individual.net...
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
"Teemu Keiski" <jo****@aspalliance.com> ha scritto nel messaggio
news:OG**************@TK2MSFTNGP12.phx.gbl...
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
...

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

"Max" <ma*******@hotmail.com> wrote in message
news:3a*************@individual.net...
>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
>



Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.