This example demonstrates the basics on how to store and retrieve data between pages using ASP.NET Session. As you may know there are lots of ways on how to pass information between pages and these includes the following:
*Sessions
*Querystrings
*Cross-Page Posting
*Cookies
*Form Submit
*Server.Transfer
In this example, I'm going to show the basics on how to store the information in Session and retrieve the value of Session in the next page..
C#
protected void Button1_Click(object sender, EventArgs e) { Session["KeyName"] = "Set Value Here"; Response.Redirect("Page2.aspx"); } //Then on Page2.aspx //On page load you can get the value you stored in the session variable protected void Page_Load(object sender, EventArgs e) { if (Session["KeyName"] != null) { //get the Session value string sValue = Session["KeyName"].ToString(); } } |
VB.NET Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Session("KeyName") = "Set Value Here" Response.Redirect("Page2.aspx") End Sub 'Then on Page2.aspx 'On page load you can get the value you stored in the session variable Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Session("KeyName") IsNot Nothing Then 'get the Session value Dim sValue As String = Session("KeyName").ToString() End If End Sub |
For more information bout Session state management then I would suggest you to refer at the following link below:
ASP.NET Session State Overview