Session State In ASP.NET Web Forms | ASP.NET Sessions | State Management
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Session_State
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
//Session.Add("user",UserTextBox.Text); //Creating Session First way
Session["user"] = UserTextBox.Text; //Creating Session Second way
Response.Redirect("WebForm2.aspx");
}
}
}
Above File is Session_State\WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Session_State
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] != null)
{
//Response.Write() is used to display Text at Top Left side in browser screen
Response.Write("Welcome " + Session["user"]);
}
else
{
Response.Redirect("WebForm1.aspx");
}
}
}
}
Above File is Session_State\WebForm2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Session_State
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] != null)
{
//Response.Write() is used to display Text at Top Left side in browser screen
Response.Write("Welcome " + Session["user"].ToString());
}
else
{
Response.Redirect("WebForm1.aspx");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["user"] != null)
{
Session["user"] = null;
Response.Redirect("WebForm1.aspx");
}
}
}
}
Above File is Session_State\WebForm3.aspx.cs
Comments
Post a Comment