ViewState Variable Vs. Session Variable

A

Arpan

Consider the following ASPX code snippet:

<script language=VB runat=server>
Sub Page_Load(obj As Object,ea As EventArgs)
If Not(Page.IsPostBack) Then
ViewState("STime")=DateTime.Now
lblMessage.Text="The time now is " & ViewState("STime")
End If
End Sub

Sub btnClick(obj As Object,ea As EventArgs)
lblMessage.Text="The time now is " & DateTime.Now
lblMessage.Text+="<br>You arrived at the previous page at: " & ViewState("STime")
End Sub
</script>
<form runat=server>
<asp:Button id="submit" Text="SUBMIT" OnClick="btnClick" runat=server/>
<asp:Label id="lblMessage" runat=server/>
</form>

As is evident from the code, the btnClick method is accessing the variable ViewState("STime") but how can the btnClick access the ViewState variable which has been created in the Page_Load method? Are ViewState variables equivalent to ASP Session variables? Can ViewState variables be accessed by any other method/function?Also can variables that have been declared & assigned values in the Page_Load method be accessed by other methods/event handlers?

Newbie......please help :)

Thanks,

Arpan
 
J

Johann MacDonagh

ViewState is a collection bag which holds key value pairs of changed control attributes. You can also utilize it to store your own values which will persist though a Page's postbacks. ViewState is a member of Page, which means it is globably accessible throughout your Page. The ViewState is loaded early on in the Page creation (life cycle), and towards the end it is encrypted and outputed into the HTML. Therefore, when you add some data to the ViewState on the first page load, that data is being stored (encrypted) in the __ViewState hidden field you'll find in the HTML source. When the page is loaded again (after you click the button), that value is decrypted and loaded into the ViewState bag, which means you'll be able to access it.

Session works differently. ASP.net by default mainstains a user's session by a cookie. Values that are stored into the Session variable are stored in the web server's memory. As long as the user maintains their state, you can use the Session object as much as you want.

When you decide to choose between the two, look at what you need to. If you want to maintain a few pieces of data across postbacks, ViewState will work fine. Session will maintain as long as the user holds their session. I've had some problems in the past with session, especially when users hit the back button. User's can also delete their cookie or leave the page open to long (resulting in a session timeout), which would clear any Session objects.

Remember to always check if the value you are getting (either from ViewState or Session) is null or not. If not, you're users will get the infamous "Object reference not sent to an instance of an object." exception.

Happy coding!
Johann MacDonagh
Consider the following ASPX code snippet:

<script language=VB runat=server>
Sub Page_Load(obj As Object,ea As EventArgs)
If Not(Page.IsPostBack) Then
ViewState("STime")=DateTime.Now
lblMessage.Text="The time now is " & ViewState("STime")
End If
End Sub

Sub btnClick(obj As Object,ea As EventArgs)
lblMessage.Text="The time now is " & DateTime.Now
lblMessage.Text+="<br>You arrived at the previous page at: " & ViewState("STime")
End Sub
</script>
<form runat=server>
<asp:Button id="submit" Text="SUBMIT" OnClick="btnClick" runat=server/>
<asp:Label id="lblMessage" runat=server/>
</form>

As is evident from the code, the btnClick method is accessing the variable ViewState("STime") but how can the btnClick access the ViewState variable which has been created in the Page_Load method? Are ViewState variables equivalent to ASP Session variables? Can ViewState variables be accessed by any other method/function?Also can variables that have been declared & assigned values in the Page_Load method be accessed by other methods/event handlers?

Newbie......please help :)

Thanks,

Arpan
 
J

Johann MacDonagh

My apologies. The VB.net equivalent to null is Nothing. You'll get compile
errors if you try to use null.

Happy coding,
Johann MacDonagh

"Johann MacDonagh msn.com>" <JohannMacDonagh<at> wrote in message
ViewState is a collection bag which holds key value pairs of changed control
attributes. You can also utilize it to store your own values which will
persist though a Page's postbacks. ViewState is a member of Page, which
means it is globably accessible throughout your Page. The ViewState is
loaded early on in the Page creation (life cycle), and towards the end it is
encrypted and outputed into the HTML. Therefore, when you add some data to
the ViewState on the first page load, that data is being stored (encrypted)
in the __ViewState hidden field you'll find in the HTML source. When the
page is loaded again (after you click the button), that value is decrypted
and loaded into the ViewState bag, which means you'll be able to access it.

Session works differently. ASP.net by default mainstains a user's session by
a cookie. Values that are stored into the Session variable are stored in the
web server's memory. As long as the user maintains their state, you can use
the Session object as much as you want.

When you decide to choose between the two, look at what you need to. If you
want to maintain a few pieces of data across postbacks, ViewState will work
fine. Session will maintain as long as the user holds their session. I've
had some problems in the past with session, especially when users hit the
back button. User's can also delete their cookie or leave the page open to
long (resulting in a session timeout), which would clear any Session
objects.

Remember to always check if the value you are getting (either from ViewState
or Session) is null or not. If not, you're users will get the infamous
"Object reference not sent to an instance of an object." exception.

Happy coding!
Johann MacDonagh
Consider the following ASPX code snippet:

<script language=VB runat=server>
Sub Page_Load(obj As Object,ea As EventArgs)
If Not(Page.IsPostBack) Then
ViewState("STime")=DateTime.Now
lblMessage.Text="The time now is " & ViewState("STime")
End If
End Sub

Sub btnClick(obj As Object,ea As EventArgs)
lblMessage.Text="The time now is " & DateTime.Now
lblMessage.Text+="<br>You arrived at the previous page at: " &
ViewState("STime")
End Sub
</script>
<form runat=server>
<asp:Button id="submit" Text="SUBMIT" OnClick="btnClick" runat=server/>
<asp:Label id="lblMessage" runat=server/>
</form>

As is evident from the code, the btnClick method is accessing the variable
ViewState("STime") but how can the btnClick access the ViewState variable
which has been created in the Page_Load method? Are ViewState variables
equivalent to ASP Session variables? Can ViewState variables be accessed by
any other method/function?Also can variables that have been declared &
assigned values in the Page_Load method be accessed by other methods/event
handlers?

Newbie......please help :)

Thanks,

Arpan
 

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

Similar Threads

ViewState/StateBag? 1
Variable Scope & Page.DataBind()? 4
ViewState? 1
UserControl? 0
viewstate 2
Business Objects? 2
Losing Session Variable 2
another problem with viewstate 5

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top