Asp.net web pages are developed are Hyper Text Transfer Protocol based (HTTP). HTTP is a stateless protocol which treats as each request as an independent transaction it is not related to previous transactions.
There are two types of state management techniques.
- Client – side state management techniques.
- Cookies - – A cookie contains a small bit of information which can be read whenever the user visits the website.
Cookies Property:
Domain – Specifies which domain is associated with this cookie. Default is the current domain.
Expires – Set the expire time for cookies
Http – To access the cookie value in Java script code.
Secure – It is set when a cookie is transmitted over SSL.
Name – Cookie Name
Value – Cookie value - Hidden fields – A hidden field contains the information on the page level.
- View State – A view state is a built in feature in web controls which contains information for a single user to persist data between the pages is post backs.
- Query strings – A Query strings is an information send along with the URL.
- Cookies - – A cookie contains a small bit of information which can be read whenever the user visits the website.
- Server – side state management techniques.
- Application Objects – An application object store information which is visible across the application and share across the entire application.
An application objects have two events - a) Application_Start – its execute when the application is started.
- b) Application_End– its execute when the application is ended.
- Session Objects – A Session Object is used to store state specific information per client basis and its specific to a particular user.
A Session object has two events
a) Session _Start – its execute when the application is started.
b) Session _End– its execute when the application is ended
Session.Abandon(); //Cancels the session and fires end event.
Session.Clear(); /Session.RemoveAll(); //Clears all contents of the session.
Session.Remove(string); //Removes the session name supplied. - Cache – A cache is stored information on the page level
Application.Lock(); // Method To Lock Appilcation Object
Application["myApplication"] = "myApplication"; // To declare Application Object
Application.UnLock(); // Method To UnLock Appilcation Object
- Application Objects – An application object store information which is visible across the application and share across the entire application.
Cookies Example
MyPage1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage1.aspx.cs" Inherits="MyPage1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" /> </div> </form> </body> </html>
MyPage1.aspx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class MyPage1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("MyHttpCookies "); Response.Cookies["MyFirstCookies"].Value = "MyCookies"; // Add For Expire Cookie after one day cookie.Expires = DateTime.Now.AddDays(1); Response.Redirect("MyPage2.aspx"); } }
MyPage2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage2.aspx.cs" Inherits="MyPage2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblShowMessage" runat="server"></asp:Label> <asp:Label ID="lblHTTPShowMessage" runat="server"></asp:Label> </div> </form> </body> </html>
MyPage2.aspx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class MyPage2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.Cookies["MyFirstCookies"] != null) lblShowMessage.Text = "Hello," + Request.Cookies["MyFirstCookies"].Value + "!"; else lblShowMessage.Text = "Sorry"; if (Request.Cookies["MyHttpCookies "] != null) lblHTTPShowMessage.Text = "Hello," + Request.Cookies["MyHttpCookies "].Value + "!"; else lblHTTPShowMessage.Text = "Sorry"; } } }
Hidden field Example
MyPage1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage1.aspx.cs" Inherits="MyPage1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" /> <asp:Label ID="lblShowMessage" runat="server"></asp:Label> <asp:HiddenField ID="HiddenField1" runat="server" /> </div> </form> </body> </html>
MyPage1.aspx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class MyPage1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { HiddenField1.Value = ""; } } protected void Button1_Click(object sender, EventArgs e) { HiddenField1.Value = "MyHiddenField"; if (HiddenField1.Value != “”) lblShowMessage.Text = "Hello," + HiddenField1.Value + "!"; else lblShowMessage.Text = "Sorry"; } }
View State Example
MyPage1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage1.aspx.cs" Inherits="MyPage1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" /> <asp:Label ID="lblShowMessage" runat="server"></asp:Label> </div> </form> </body> </html>
MyPage1.aspx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class MyPage1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["MyFirstViewState"] = ""; } } protected void Button1_Click(object sender, EventArgs e) { ViewState["MyFirstViewState"] = "MyViewState";if (ViewState["MyFirstViewState"]!= “”) lblShowMessage.Text = "Hello," + ViewState["MyFirstViewState"] + "!"; else lblShowMessage.Text = "Sorry"; } }
Query String Example
MyPage1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage1.aspx.cs" Inherits="MyPage1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" /> </div> </form> </body> </html>
MyPage1.aspx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class MyPage1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("MyPage2.aspx?QueryString='MyFirstQueryString'"); } }
MyPage2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage2.aspx.cs" Inherits="MyPage2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblShowMessage" runat="server"></asp:Label> </div> </form> </body> </html>
MyPage2.aspx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class MyPage2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString["QueryString"] != null) lblShowMessage.Text = "Hello," + Request.Cookies["QueryString"].Value + "!"; else lblShowMessage.Text = "Sorry"; } } }
Application Object Example
MyPage1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage1.aspx.cs" Inherits="MyPage1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" /> </div> </form> </body> </html>
MyPage1.aspx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class MyPage1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } protected void Button1_Click(object sender, EventArgs e) { Application["MyFirstApplicationObject"] = "MyApplicationObject"; Response.Redirect("MyPage2.aspx"); } }
MyPage2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage2.aspx.cs" Inherits="MyPage2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblShowMessage" runat="server"></asp:Label> </div> </form> </body> </html>
MyPage2.aspx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class MyPage2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Session ["MyFirstSessionObject"] != null) lblShowMessage.Text = "Hello," + Session["MyFirstSessionObject"] + "!"; else lblShowMessage.Text = "Sorry"; } } }
No comments:
Post a Comment