Session and View state in ASP. NET Using VB.NET

This article defines the session and view state in ASP.NET.
  • 4795
Download Files:
 

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 ObjectByVal e As System.EventArgsHandlesMe.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 ObjectByVal e As EventArgsHandles 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 ObjectByVal e As System.EventArgsHandlesMe.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 ObjectByVal e As System.EventArgsHandlesMe.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 ObjectByVal e As EventArgsHandles 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 ObjectByVal e As System.EventArgsHandlesMe.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.

se4.gif
 

Figure4

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.