Z 3 ) State Manangement
State Manangement
- Application state management is another technique for maintaining the state on the server-side. The data persisting in the application state is shared by all users and that data can be accessed from anywhere within the application.
Source Code :- Ac1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ac1.aspx.cs" Inherits="WebApplication1.Ac1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
Email
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Height="29px" OnClick="Button1_Click" Text="Send" Width="75px" />
</div>
</form>
</body>
</html>
Button Code :- Ac1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Ac1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//create session
Session["email"] = TextBox1.Text;
Session.Timeout = 4; // error object refrence not found
Response.Redirect("Ac3.aspx");
}
}
}
Q )How to Destroy Session
2 Method for destroy session
- Clear :- Just Clear the session value.
- Abandon :- Clear the memory location
Source Code :- Ac2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ac2.aspx.cs" Inherits="WebApplication1.Ac2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Logout" />
</div>
</form>
</body>
</html>
Button Code :- Ac2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Ac2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = (string)Session["email"];
if (Session["email"] == null)
{
Response.Redirect("Ac1.aspx");
}
else
{
if (!IsPostBack)
{
}
}
}
protected void show()
{
Session.Clear();
Session.Abandon();
Response.Redirect("Ac1.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
show();
}
}
}
Source Code :- Ac3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ac3.aspx.cs" Inherits="WebApplication1.Ac3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="logout" />
</div>
</form>
</body>
</html>
Button Code :- Ac3.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Ac3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = (string)Session["Email"];
if (Session["Email"] == null)
{
Response.Redirect("Ac1.aspx");
}
else
{
if (!IsPostBack)
{
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
// Session.Clear();
// Session.Abandon();
Response.Redirect("Ac1.aspx");
}
}
}
Q ) how to increase Session Time
- By Default it is a 60 sec
Step :- Web.Confige
<system.web>
<sessionState mode="InProc" cookieless="true" timeout="60"/>
</system.web>
Step :- session create ke niche
Code :- Session.Timeout = 4 ;
View State
- ViewState is use to data on single page.
- View state work on client side
- Session is on server side
- Session store data on whole website
Source Code :- ViewwState.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Bvstate.aspx.cs" Inherits="WebApplication1.vstate" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title></title></head><body><form id="form1" runat="server"><div> <br /><br /><br /> Enter data value<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br /> <asp:Button ID="Button1" runat="server" Height="29px" OnClick="Button1_Click" Text="Clear Value" Width="73px" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Display Value" /><br /><br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /></div></form></body></html>
Button Code :- ViewwState.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace WebApplication1
{
public partial class vstate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Q ) How To create View State ?
ViewState["name"] = TextBox1.Text;
TextBox1.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
Q ) How To GetView State ?
Label1.Text = ViewState["name"].ToString();
}
}
}
Context
- Contex is also called State Mangement
- Context Management is use to server transfer method .
Source Code :- Context1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aaContext.aspx.cs" Inherits="WebApplication1.aaContext" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
Enter data
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" />
</div>
</form>
</body>
</html>
Button Code :- Context1.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class aaContext : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// Context.Items[""] = Controlname;
//Server.Transfer("pagename.aspx");
// Q ) how to create context
Context.Items["email"] = TextBox1.Text;
Server.Transfer("aaContext2.aspx");
}
}
}
Source Code :- Context2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aaContext2.aspx.cs" Inherits="WebApplication1.aaContext2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Welcome User
<asp:Label ID="Label1" runat="server" BackColor="#FF99FF" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Button Code :- Context2.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class aaContext2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Q ) How to retrive context value
//Controle = Context.Items["variable"].ToString();
Label1.Text = Context.Items["email"].ToString();
}
}
}
Cookies
- It is a state management
- All cookies information is to be store user hard drive
- Cookies dosn't use server memory
- Cookies to be expire as per requirement
Source code :- Cookie.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookies.aspx.cs" Inherits="WebApplication1.Cookies" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<p>
<br />
</p>
<p>
</p>
<form id="form1" runat="server">
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Create cookies" />
</p>
<p>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Retrive Cookies" />
</p>
<p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
<div>
</div>
</form>
</body>
</html>
Button code :- Cookie.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Cookies : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//Q ) How to create cookies
Response.Cookies["name"].Value = TextBox1.Text;
Response.Cookies["name"].Expires = DateTime.Now.AddMinutes(1);
Label1.Text = "cookies created";
TextBox1.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
if(Request.Cookies["name"]==null)
{
TextBox2.Text = "no cooies found";
}
else
{
//Q ) How to retrive cookies
TextBox2.Text = Request.Cookies["name"].Value;
}
}
}
}
Application State
- Server side state management
- It is a global storage concept application state data accesable any where
- Syntax :- Application ["Variable"] = Value;
Source Code :- ApplicationState.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Applicatstate.aspx.cs" Inherits="WebApplication1.Applicatstate" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Height="31px" OnClick="Button1_Click" Text="Click to visit" Width="117px" />
</div>
</form>
</body>
</html>
Button Code :- ApplicationState.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Applicatstate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int count = 0;
if(Application["visit"]!=null)
{
count = int.Parse(Application["visit"].ToString());
}
count = count + 1;
Application["visit"] = count; //replace
Label1.Text =count.ToString(); //print data
}
}
}
Comments
Post a Comment