Server Side Technique(Managing state)
1) Session State
Session state objects are created
when a user request for the ASP.NET when the session state it turned on.
Session objects are created different for all the new requests. It can
work across all the pages that means we can access the session object in
all the pages. This was not possible with viewstate variables.. It is
mainly used to store user details like login name. It can also be used
to store shopping cart details, customer details etc.
Session objects consists of session ID's which is uniques for each new
requests. Session ID objects are created from HttpSessionState class.
Bydefault, an ASP.NET session state is enabled for all ASP.NET applications. Session variables are called server side technique because they are stored in the web server memory.
In short we can say, session variables are single-user global data stored on the web server which can be accessed across all the web pages.
Let us look at the same example used in viewstate.
Design
SessionState.aspx page
In
this the change i have done is the submit button which will redirect to
a new page(i.e. NewPage.aspx). This will help us to show whether it is
accessible across all the pages.Bydefault, an ASP.NET session state is enabled for all ASP.NET applications. Session variables are called server side technique because they are stored in the web server memory.
In short we can say, session variables are single-user global data stored on the web server which can be accessed across all the web pages.
Let us look at the same example used in viewstate.
Design
SessionState.aspx page
NewPage.aspx page
Code Behind
SessionState.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace httpstateless
{
public partial class SessionState : System.Web.UI.Page
{
string name, blog;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
name = TextBox1.Text;
blog = TextBox2.Text;
TextBox1.Text = "";
TextBox2.Text = "";
Session["name"] = name;
Session["blog"] = blog;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = Convert.ToString(Session["name"]);
TextBox2.Text = Convert.ToString(Session["blog"]);
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("NewPage.aspx");
}
}
}
NewPage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace httpstateless
{
public partial class NewPage : System.Web.UI.Page
{
string name, blog;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
name = TextBox1.Text;
blog = TextBox2.Text;
TextBox1.Text = "";
TextBox2.Text = "";
Session["name"] = name;
Session["blog"] = blog;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = Convert.ToString(Session["name"]);
TextBox2.Text = Convert.ToString(Session["blog"]);
}
}
}
Output
After entering the details click on save button to save the content in session state variables. You may use the restore button to check whether it is working fine. After that click on submit button to move to the new page.
In the new page click on restore button to restore the content from the session state variable and it will work fine as session state variables can be accessed across pages.
NewPage.aspx page
After clicking on restore button.