473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ViewState v Session - Confused

I am testing for how/when a page is posted back and I decided to use a
ViewState variable in PageLoad to set up a counter, but it appears,
the ViewState is cleared on each PageLoad. So then I used SESSION and
that worked.

Am I correct in assuming ViewState is cleared on each PageLoad or is my
code incorrect.

VIEWSTATE
If IsNothing(ViewState("PbCounter")) Then

ViewState("PBCounter") = 1
Else
ViewState("PBCounter") = Session("PBCounter") + 1
End If

SESSION
If IsNothing(Session("PbCounter")) Then

Session("PBCounter") = 1
Else
Session("PBCounter") = Session("PBCounter") + 1
End If

Response.write(ViewState("PBCounter").tostring) ALWAYS ONE

Response.write(Session("PBCounter").tostring) INCREMENTS CORRECTLY

Thanks - Richard

May 10 '06 #1
6 1578
If this is really your code, then you are putting an empty Session
variable into the Viewstate.

VIEWSTATE
If IsNothing(ViewState("PbCounter")) Then
ViewState("PBCounter") = 1
Else
ViewState("PBCounter") = Session("PBCounter") + 1 <<<<<<<
HERE
End If

May 10 '06 #2
Sorry Typo - with cut and paste, no it was correct in the actual code.
I did viewstate first and when it did not work I then changed it to
session.

May 10 '06 #3
ViewState is stored in a Page, and passed back and forth to the client via a
hidden form field. Session is stored in Server memory. Therefore, ViewState
is scoped to a Page, but only to the Page after PostBack, not when it is
initially loaded. Session State is scoped to a single user Session, and
transcends Pages.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"mosscliffe" <pa***********@googlemail.com> wrote in message
news:11********************@j73g2000cwa.googlegrou ps.com...
I am testing for how/when a page is posted back and I decided to use a
ViewState variable in PageLoad to set up a counter, but it appears,
the ViewState is cleared on each PageLoad. So then I used SESSION and
that worked.

Am I correct in assuming ViewState is cleared on each PageLoad or is my
code incorrect.

VIEWSTATE
If IsNothing(ViewState("PbCounter")) Then

ViewState("PBCounter") = 1
Else
ViewState("PBCounter") = Session("PBCounter") + 1
End If

SESSION
If IsNothing(Session("PbCounter")) Then

Session("PBCounter") = 1
Else
Session("PBCounter") = Session("PBCounter") + 1
End If

Response.write(ViewState("PBCounter").tostring) ALWAYS ONE

Response.write(Session("PBCounter").tostring) INCREMENTS CORRECTLY

Thanks - Richard

May 10 '06 #4
Thanks. That was helpful information.

--
Greg Collins [InfoPath MVP]
Visit Brain Trove ( http://www.BrainTrove.com )
Visit InfoPath Dev ( http://www.InfoPathDev.com )
May 10 '06 #5
Eh?

On first page request you should be able to add custom values.
On postback you should be able to obtain it again.
Am i missing something here, i believe i thave tested this a while ago.
Of course a viewstate is bounded to a page but that could be welcome.
+ when using a master page you could tag stuff to multiple 'pages' (imo)

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> schreef in bericht
news:ek**************@TK2MSFTNGP05.phx.gbl...
ViewState is stored in a Page, and passed back and forth to the client via
a hidden form field. Session is stored in Server memory. Therefore,
ViewState is scoped to a Page, but only to the Page after PostBack, not
when it is initially loaded. Session State is scoped to a single user
Session, and transcends Pages.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"mosscliffe" <pa***********@googlemail.com> wrote in message
news:11********************@j73g2000cwa.googlegrou ps.com...
I am testing for how/when a page is posted back and I decided to use a
ViewState variable in PageLoad to set up a counter, but it appears,
the ViewState is cleared on each PageLoad. So then I used SESSION and
that worked.

Am I correct in assuming ViewState is cleared on each PageLoad or is my
code incorrect.

VIEWSTATE
If IsNothing(ViewState("PbCounter")) Then

ViewState("PBCounter") = 1
Else
ViewState("PBCounter") = Session("PBCounter") + 1
End If

SESSION
If IsNothing(Session("PbCounter")) Then

Session("PBCounter") = 1
Else
Session("PBCounter") = Session("PBCounter") + 1
End If

Response.write(ViewState("PBCounter").tostring) ALWAYS ONE

Response.write(Session("PBCounter").tostring) INCREMENTS CORRECTLY

Thanks - Richard


May 10 '06 #6
Yes, you can add values to it initially, but it is not stored. It is stored
in a hidden form field, which, when the page Posts Back, re-creates the
ViewState object from the field contents. In other words, if you add
something to a Page's ViewState, and navigate back to the same Page by any
means other then a PostBack, the ViewState will be empty.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Edwin Knoppert" <in**@pbsoft.speedlinq.nl> wrote in message
news:e3**********@azure.qinip.net...
Eh?

On first page request you should be able to add custom values.
On postback you should be able to obtain it again.
Am i missing something here, i believe i thave tested this a while ago.
Of course a viewstate is bounded to a page but that could be welcome.
+ when using a master page you could tag stuff to multiple 'pages' (imo)

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> schreef in bericht
news:ek**************@TK2MSFTNGP05.phx.gbl...
ViewState is stored in a Page, and passed back and forth to the client
via a hidden form field. Session is stored in Server memory. Therefore,
ViewState is scoped to a Page, but only to the Page after PostBack, not
when it is initially loaded. Session State is scoped to a single user
Session, and transcends Pages.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"mosscliffe" <pa***********@googlemail.com> wrote in message
news:11********************@j73g2000cwa.googlegrou ps.com...
I am testing for how/when a page is posted back and I decided to use a
ViewState variable in PageLoad to set up a counter, but it appears,
the ViewState is cleared on each PageLoad. So then I used SESSION and
that worked.

Am I correct in assuming ViewState is cleared on each PageLoad or is my
code incorrect.

VIEWSTATE
If IsNothing(ViewState("PbCounter")) Then

ViewState("PBCounter") = 1
Else
ViewState("PBCounter") = Session("PBCounter") + 1
End If

SESSION
If IsNothing(Session("PbCounter")) Then

Session("PBCounter") = 1
Else
Session("PBCounter") = Session("PBCounter") + 1
End If

Response.write(ViewState("PBCounter").tostring) ALWAYS ONE

Response.write(Session("PBCounter").tostring) INCREMENTS CORRECTLY

Thanks - Richard



May 10 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: neo | last post by:
hi, I am studying ASP.NET and have few questions - 1) The session ID and values of controls is stored in VIEWSTATE variable. So now when we put EnableViewState="false" in Page directive and...
4
by: Chuck Ritzke | last post by:
Hi, I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle...
6
by: clsmith66 | last post by:
Is it possible to store the same information about a control that would be saved in the ViewState in a Session state? I have a page with three treeview controls and if I enable the view state for...
3
by: Alex Nitulescu | last post by:
Sorry, I'm really confused - I'm only at the middle of my book and already I can count 4 methods to store values. Yes, I know that cookies can not store much, but what is the difference between...
3
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and...
9
by: Mark Broadbent | last post by:
Been a while since I've touched asp.net but one thing that always seems to fustrate me is the loss of state on variable declarations. Is there anyway (i.e. assigning an attribute etc) to instruct...
1
by: Hugo Flores | last post by:
Hi, I'm trying to pass information between pages. I'm trying to avoid using QueryString and Session variables. I saw somewhere that I could use contxt.Items.Add (combined with Server.Transfer)...
1
by: cheloman12 | last post by:
Hi, I posted this problem some days ago. I repeat it because it was no answered. If description is confused, please make me know and i'll try to explain the case better. Thanks a lot, Marcelo...
2
by: digbydog | last post by:
Hello, I am a bit confused with viewstate - am trying to use viewstate to hold a boolean value which is false initially and then gets set to true after a certain event. I had the following ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.