2.0: Session variables vs class-page fields

R

RAM

Hello,
I am learning ASP.NET 2.0. Could you explain me please the difference when
to use fields of a System.Web.UI.Page-derived class and whem I must use
Session variable instead. For example I have a class PZ : System.Web.UI.Page
and I need to remember CurrentPosition (int) of asp:DataList. Can I use
private field of PZ class (even in multiuser ASP.NET application) or I have
to use Session["CurrentPosition"]? For unknown reason I have null in
PositionsTable (DataTable, field) after next page unload/load although I
assign non-null object to PositionsTable in some event-handler function.
Please help. Thank you!
/RAM/
 
F

Flinky Wisty Pomm

An instance of your page class is created and destroyed for each
request. The values of class field members are not persisted across
requests and must be placed in a longer-lived mechanism if you want to
keep them.

For something like the selected element in a DataList, your best bet is
ViewState["myKey"] = myIntegerPosition.

That's how the Asp.Net controls store info across postbacks and, used
sensibly, it's a decent way of storing info. Don't stick anything huge
in there though, because it will be sent/received for each request as
part of the generated HTML.
 
R

RAM

Thanks a lot!

Uzytkownik "Flinky Wisty Pomm said:
An instance of your page class is created and destroyed for each
request. The values of class field members are not persisted across
requests and must be placed in a longer-lived mechanism if you want to
keep them.

For something like the selected element in a DataList, your best bet is
ViewState["myKey"] = myIntegerPosition.

That's how the Asp.Net controls store info across postbacks and, used
sensibly, it's a decent way of storing info. Don't stick anything huge
in there though, because it will be sent/received for each request as
part of the generated HTML.
Hello,
I am learning ASP.NET 2.0. Could you explain me please the difference
when
to use fields of a System.Web.UI.Page-derived class and whem I must use
Session variable instead. For example I have a class PZ :
System.Web.UI.Page
and I need to remember CurrentPosition (int) of asp:DataList. Can I use
private field of PZ class (even in multiuser ASP.NET application) or I
have
to use Session["CurrentPosition"]? For unknown reason I have null in
PositionsTable (DataTable, field) after next page unload/load although I
assign non-null object to PositionsTable in some event-handler function.
Please help. Thank you!
/RAM/
 

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

Members online

Forum statistics

Threads
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top