2.0: Session variables vs class-page fields

  • Thread starter Thread starter RAM
  • Start date Start date
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/
 
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.
 
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/
 
Back
Top