ARTICLE

Session and View state in ASP. NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET June 07, 2011
This article defines the session and view state in ASP.NET.  
Download Files:
 
Reader Level:

Session and View state in ASP. NET

This article defines the session and view state in ASP.NET.  

Session

A session is defined as the period of time that a unique user intracts with a web application.

Session variables are stored in a SessionStateItemCollection object that is exposed through the HttpContext.Session property.

Advantages:

  • We can use some existing Table for the store session data, It is useful when we have to use some old database rather than SQL Server.

  • It's not depending on IIS , So Restarting web server does not make any effects on session data.

  • We can crate our own algorithm for generating Session ID.

For Example

Drag and drop one button control and level control on the form. Double click on the form and add the following code.

Public Class WebForm1

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            Session("StartTime") = DateTime.Now

            lblMessage.Text = "The time is now: " & Session("StartTime")

        End If

    End Sub

    Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs) Handles btSubmit.Click

        lblMessage.Text = "The time is now: " & DateTime.Now & "<br>started at:" & Session("StartTime")

        Response.Redirect("webform2.aspx")

    End Sub

End Class

 

Adding one more form(webform2) in application and add the Following code.

Public Class WebForm2

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Label1.Text = "time is: " & Session("StartTime")

    End Sub

End Class

Now run the application.

se1.gif

Figure1

Now click on the Button to Redirect the webform2.

se2.gif

Figure2

View state

Special object to manage information for current web page between different calls of same web page.

A hidden variable called _VIEWSTATE is used to hold the values of View State object and resubmit on every click.

View state is enabled by default so if you view a web form page in your browser you will see a line similar to the following near the form definition in your rendered HTML.

Now apply same code for view state as above for session. Now replace view state in place of session.

Public Class WebForm1

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            ViewState("StartTime") = DateTime.Now

            lblMessage.Text = "The time is now: " & ViewState("StartTime")

        End If

    End Sub

    Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs) Handles btSubmit.Click

        lblMessage.Text = "The time is now: " & DateTime.Now & "<br>started at:" & ViewState("StartTime")

        Response.Redirect("webform2.aspx")

    End Sub

End Class

 

Adding one more form(webform2) in application and add the Following code.

Public Class WebForm2

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Label1.Text = "time is: " & ViewState("StartTime")

    End Sub

End Class

Now run the application.

se4.gif

Figure3

Now click on the Button to Redirect the webform2.

se3.gif

Figure4

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Diagram
Become a Sponsor