473,721 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ViewS tate("PbCounter ")) Then

ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 1
End If

SESSION
If IsNothing(Sessi on("PbCounter") ) Then

Session("PBCoun ter") = 1
Else
Session("PBCoun ter") = Session("PBCoun ter") + 1
End If

Response.write( ViewState("PBCo unter").tostrin g) ALWAYS ONE

Response.write( Session("PBCoun ter").tostring ) INCREMENTS CORRECTLY

Thanks - Richard

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

VIEWSTATE
If IsNothing(ViewS tate("PbCounter ")) Then
ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 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******** ************@j7 3g2000cwa.googl egroups.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(ViewS tate("PbCounter ")) Then

ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 1
End If

SESSION
If IsNothing(Sessi on("PbCounter") ) Then

Session("PBCoun ter") = 1
Else
Session("PBCoun ter") = Session("PBCoun ter") + 1
End If

Response.write( ViewState("PBCo unter").tostrin g) ALWAYS ONE

Response.write( Session("PBCoun ter").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***@DIESPAMM ERSDIEtakempis. com> schreef in bericht
news:ek******** ******@TK2MSFTN GP05.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******** ************@j7 3g2000cwa.googl egroups.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(ViewS tate("PbCounter ")) Then

ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 1
End If

SESSION
If IsNothing(Sessi on("PbCounter") ) Then

Session("PBCoun ter") = 1
Else
Session("PBCoun ter") = Session("PBCoun ter") + 1
End If

Response.write( ViewState("PBCo unter").tostrin g) ALWAYS ONE

Response.write( Session("PBCoun ter").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.sp eedlinq.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***@DIESPAMM ERSDIEtakempis. com> schreef in bericht
news:ek******** ******@TK2MSFTN GP05.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******** ************@j7 3g2000cwa.googl egroups.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(ViewS tate("PbCounter ")) Then

ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 1
End If

SESSION
If IsNothing(Sessi on("PbCounter") ) Then

Session("PBCoun ter") = 1
Else
Session("PBCoun ter") = Session("PBCoun ter") + 1
End If

Response.write( ViewState("PBCo unter").tostrin g) ALWAYS ONE

Response.write( Session("PBCoun ter").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
2275
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 disable the session state in Web.Config the VIEWSTATE variable is still maintained and stores some values. Can anyone tell what those values are for, i.e what other info is stored in VIEWSTATE other than the session ID and the control values ?
4
6729
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 from all the various explanations/answers. I'm attempting the typical scenario... I create a variable number of controls at runtime based on parameters from a database.
6
4344
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 the controls I get huge delays on postbacks, but if I disable the viewstate I lose all my information. Any sugesstions?
3
4111
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 storing data in the cache object, in a viewstate and in a Session. I can say Cache("MyData")=objDataSet as well as
3
4572
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 set textboxes, radio buttons, etc - that viewstate is fine. Then I have a linkbutton that does a Server.Transfer over to Page2.aspx. When I Server.Transfer back to Page1.aspx, the viewstate info is lost. I ran across another example of this last...
9
1870
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 the server to remember a variables state *without* having to go through the rigmarole of saving and loading to and from the Session state manually or similar workaround for any Types (including custom types) in exactly the same way web controls...
1
1449
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) to store my information and send it to another page. Some of my pages, either go to other pages, or postback depending on the situation. When I began to get confused was when I saw that I needed to store my variables on the viewstate if I wanted...
1
2768
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 "I’m implementing SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium in asp.net 2.0 to increment performance
2
246
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 Class ....
0
8727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9365
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9127
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2569
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.