ASP .NET - Viewstate and Sessions... they're much more than strings
In ASP .NET, whenever you make use of data storage techniques, sometimes you're confined to using string datatypes. For example, whenever you store information in cookies, the data transmitted is a string. However, ASP .NET provides a lot more variety than just strings. Using Sessions and the ASP .NET ViewState the data may look like a string, but it is in fact much more than that.
Note to the reader: This article will discuss in some detail how to store data into the ASP .NET Session State and ViewState. Like most things in the programming world, just because you can do it, doesn't mean you should do it. Session and ViewState are powerful data transfer mechanism's, but keep in mind that the larger these objects are, the more taxing your applications are against your server's. Therefore, take great care of how liberally you use such objects.
Sample Data
For Session and ViewState Objects, let's create an example. Let's say you have a Hashtable carrying your user data:
Dim hashUserData As New System.Collections.Hashtable
hashUserData.Add("FName", "John")
hashUserData.Add("LName", "Jones")
hashUserData.Add("EmployeeID", "00123456")
The data above is the data you need carried across page views.
Session Objects
Read the above line slowly... it's not Session "Strings", it's Session "Objects". If we have an object of data, such as our HashTable, we can store such data in a Session Object
Dim hashUserData As New System.Collections.Hashtable
hashUserData.Add("FName", "John")
hashUserData.Add("LName", "Jones")
hashUserData.Add("EmployeeID", "00123456")
Session("ourUserData") = hashUserData
Now, if you want to carry this employee data to another page and reference this data, just set this data to a Hashtable and use at will...
If Session("ourUserData") <> "" Then
Dim userData As New System.Collections.Hashtable = Session("ourUserData")
Dim FName as String = userData.Item("FName")
...
...And So On...
...
End If
ViewState Objects
Another handy feature of ASP .NET is the ViewState. For those of you who aren't familiar with this concept, view the source of an ASP .NET webpage. In there, you will see a hidden field called "__ViewState", and its value will look something like this "AHGIE6AFDFGEDSFG%...". The ViewState data looks like that because it hold the current state of the page, encrypted, for form postback's that the application may incur. In other words, if you have ever used the "AutoPostBack" feature on ASP .NET Web Controls, and wondered how the data remains intact on the page.. this is the magic behind all of it. (Or to quote the movie Swingers: "This is the guy, behind the guy, behind the guy").
Much like Session Objects, which we discussed above, the ViewState also stores data as Objects. Meaning, of course, that if we wanted to store our HashTable in our pages ViewState, we can.
For some basic logic, we'll say that our logic to get/receive user info is all within the Page_Load procedure of our ASP .NET page:
If Not Page.IsPostBack Then
Dim hashUserData As New System.Collections.Hashtable
hashUserData.Add("FName", "John")
hashUserData.Add("LName", "Jones")
hashUserData.Add("EmployeeID", "00123456")
ViewState.Add("ourUserData", hashUserData)
Else
Dim ourUserData As New System.Collections.Hashtable
ourUserData = ViewState.Item("ourUserData")
Response.Write(ourUserData.Count.ToString)
End If
As you can see in the example above, the data is stored into the ASP .NET ViewState for the page, and on all subsequent page requests, the data is available.
In Conclusion
In conclusion, hopefully the 2 brief and simple examples give you some idea as to how you can carry enhanced datatypes from page view to page view with some simplicity.


0 Comments:
Post a Comment
<< Home