Application State In ASP.NET Web Forms | State Management | ASP.NET Web Forms
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
Application["user"] = UserTextBox.Text;
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)
{
//Usage of Session as shown below
//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");
//}
//Usage of Application State as shown below
if (Application["user"] != null)
{
Response.Write("Welcome " + Application["user"].ToString());
}
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)
{
//Usage of Session as shown below
//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");
//}
//Usage of Application State as shown below
if (Application["user"] != null)
{
Response.Write("Welcome " + Application["user"].ToString());
}
else
{
Response.Redirect("WebForm1.aspx");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//Usage of Session as shown below
//if (Session["user"] != null)
//{
// Session["user"] = null;
// Response.Redirect("WebForm1.aspx");
//}
//Usage of Application State as shown below
if (Application["user"] != null)
{
Application["user"] = null;
Response.Redirect("WebForm1.aspx");
}
}
}
}
Above File is Session_State\WebForm3.aspx.cs
Comments
Post a Comment