SlideShare a Scribd company logo
NITHIYAPRIYA PASAVARAJ
ASP.NET–
STATE MANAGEMENT TECHNIQUES
ASP.NET - STATEMANAGEMENT
 State Management is very important feature in
ASP.NET.
 State Management maintains & stores the information
of any user till the end of the user session.
 State management can be classified into two types.
 Client side state management
 Server side state management
Client side state management
Client side state management can
be handled by
 Hidden Fields
 View State
 Cookies
 Query String
 Control state
Server side state Management
Server side state management can
be handled by
 Session state
 Application state
Hidden Field control
 Hidden Field is a control provided by ASP.NET,
which is used to store small amount of data on the
client
 Hidden Field Control is not rendered to the browser
and it is invisible on the browser.
Syntax:
<asp: HiddenField ID=“HiddenField1”
runat=“server/>
Hidden Field Control Example
HiddenField.aspx
HiddenField.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class HiddenField : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (HiddenField1.Value != null)
{
int val = Convert.ToInt32(HiddenField1.Value) + 1;
HiddenField1.Value = val.ToString();
Label1.Text = "The Hidden Field Value is Incremented by 1 & the Current Value is : <B>" +
val.ToString() + "</B>";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Visible = true;
}
}
Asp.net state management
ViewState
 View state is used to store user’s data, which is provided by
Asp.net client side state management mechanism.
 ViewState stores data in the generated HTML using hidden
field , not on the server.
 ViewState provides page level state management.
 ie., as long as the user is on the current page, the state will be available;
Once the user redirects to the next page , the current state will be lost.
 ViewState can store any type of data & it will be enabled by
default for all the serverside control by setting true to
“EnableViewState” property.
ViewState.aspx
Viewstate.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (ViewState["count"] != null)
{
int viewstatecount = Convert.ToInt32(ViewState["count"]) + 1;
Label2.Text = viewstatecount.ToString();
ViewState["count"] = viewstatecount.ToString();
}
else
{
ViewState["count"] = "1";
}
}
}
protected void btn_Store_Click(object sender, EventArgs e)
{
ViewState["name"] = txtName.Text;
txtName.Text = "";
Label2.Text=ViewState["count"].ToString();
}
protected void btn_Display_Click(object sender, EventArgs e)
{
Label1.Text =ViewState["name"].ToString();
Label2.Text =ViewState["count"].ToString();
}
Asp.net state management
Asp.net state management
QUERYSTRING
 A Querystring is a collection of character input to a
computer or a browser.
 In Querystring , we can sent value to only desired page & the
value will be temporarily stored.
 Querystring increase the overall performance of web
application.
 When we pass content between webforms, the Querystring
followed with a separating character (?).
 The question mark(?) is, basically used for identifying data
appearing after the separating symbol.
Example : QueryHome.aspx
QueryHome.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class QueryHome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_submit_Click(object sender, EventArgs e)
{
Response.Redirect("QueryWelcome.aspx?firstname="+TextBox1.Text+
“lastname="+TextBox2.Text);
}
}
QueryWelcome.aspx
QueryWelcome.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class QueryWelcome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String firstname = Request.QueryString["firstname"];
String lastname= Request.QueryString["lastname"];
Label1.Text = "Welcome " + firstname + " " + lastname;
}
}
Asp.net state management
Asp.net state management
COOKIES
 Cookies are small piece of text which is stored on the client’s
computer by the browser.
 i.e. ., Cookies allows web applications to save user’s
information to reuse , if needed.
Cookies can be classified into 2 types
1. Persistence Cookie
2. Non-Persistence Cookie
1. Persistence Cookie
 This types of cookies are permanently stored on user hard drive.
Cookies which have an expiry date time are called persistence cookies.
 This types of cookies stored user hard drive permanently till the date time we
set.
To create persistence cookie:
Response.Cookies[“name”].Value = “Nithiyapriya”;
Response.Cookies[“Nithiyapriya”].Expires = DateTime.Now.AddMinutes(2);
(or)
HttpCookie strname = new HttpCookie(“name”);
strname.Value = “Nithiyapriya”;
strname.Expires = DateTime.Now.AddMinutes(2);
Response.Cookies.Add(strname);
Here, the Cookie Expire time is set as 2 minutes; so that we can access the
cookie values up to 2 minutes, after 2 minutes the cookies automatically
expires.
2. Non-Persistence Cookie
 This types of cookies are not permanently stored on user hard
drive.
 It stores the information up the user accessing the same browser.
 When user close the browser the cookies will be automatically
deleted.
To create non-persistence cookie:
Response.Cookies[“name”].Value = “Nithiyapriya”;
(OR)
HttpCookie strname = new HttpCookie(“name”);
strname.Value = “Nithiyapriya”;
Response.Cookies.Add(strname);
Example
Cookies.aspx
Cookies.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Cookies : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["BackgroundColor"] != null)
{
ColorSelector.SelectedValue=Request.Cookies["BackgroundColor"].Value;
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
}
}
protected void ColorSelector_SelectedIndexChanged(object sender, EventArgs e)
{
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
HttpCookie cookie = new HttpCookie("BackgroundColor");
cookie.Value = ColorSelector.SelectedValue;
cookie.Expires = DateTime.Now.AddMinutes(2);
Response.SetCookie(cookie);
}
}
This background Color of this page retains,
until 2 minutes, even after you closed the
browser.
Server side State Management
Session State
 The session is the important features of asp.net
 Session state is used to store value for the particular period of
time.
 Session can store the client data on the sever separately for
each user & it keeps value across multiple pages of website.
 i.e. user can store some information in session in one page &
it can be accessed on rest of all other pages by using session.
 For example ,When a user login to any website with
username & password, the username will be shown to all
other pages.
Syntax :
Session[“session_name”] = “session value”;
To Declare session in asp.net
Session[“name”]=”Nithiyapriya”;
Response.Redirect(“Welcomepage.aspx”);
To Retrieve session value onWelcomepage
string myvalue= Session[“name”].ToString();
Response.Write(“Name = ” + myvalue);
Example:
To set SessionTimeout add this code toWeb.config
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<sessionState timeout="1"></sessionState>
</system.web>
Login.aspx
Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (txtusername.Text == "admin" && txtpassword.Text == "admin")
{
Session["uname"] = txtusername.Text;
Response.Redirect("Main.aspx");
} else
{
Label1.Text = "Wrong UserName/Password";
} } }
Main.aspx
Main.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["uname"] == null)
{
// Response.Redirect("Login.aspx");
Label1.Text = "Your Session is Expired";
} else
{
Label1.Text = "Welcome " + Session["uname"].ToString();
} }
protected void btn_logout_Click(object sender, EventArgs e)
{
Session["uname"] = null;
Response.Redirect("Login.aspx");
} }
After 1 Minute of Idle this
page will automatically
signed out.
Application State
 Application state is server side state management
mechanism.
 Application state is used to store data on server & shared for
all the users and it can be accessible anywhere in the
application.
 The application state is used same as session, but the
difference is, the session state is specific for a single user ;
where as an application state is common for all users.
To Store value in application state
Application[“name”] = “Nithiyapriya”;
To get value from application state
string str = Application[“name”].ToString();
 Example:
 Here we are going to calculate how many times a given page has
been visited by various clients.
Applicationstate.aspx
ApplicationState.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ApplicationState : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
int count = 0;
if (Application["visit"] != null)
{
count = Convert.ToInt32(Application["visit"].ToString());
}
count = count + 1;
Application["visit"] = count;
Label1.Text = "This page is been visited for <b> " + count.ToString() + " </b>times";
}
}
Asp.net state management

More Related Content

What's hot (20)

PPTX
ASP.NET State management
Shivanand Arur
 
PPTX
Event In JavaScript
ShahDhruv21
 
PPSX
Javascript variables and datatypes
Varun C M
 
PPTX
Javascript 101
Shlomi Komemi
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
PPTX
Css selectors
Parth Trivedi
 
PPTX
Windowforms controls c#
prabhu rajendran
 
PPT
SQLITE Android
Sourabh Sahu
 
PDF
Introduction to HTML5
Gil Fink
 
PDF
Introduction to asp.net
SHADAB ALI
 
ODP
Introduction of Html/css/js
Knoldus Inc.
 
PPTX
Angularjs PPT
Amit Baghel
 
PPTX
ASP.NET MVC.
Ni
 
PPT
Introduction to JavaScript (1).ppt
MuhammadRehan856177
 
PPTX
Lab #2: Introduction to Javascript
Walid Ashraf
 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
PPT
Java Servlets
BG Java EE Course
 
PPT
android layouts
Deepa Rani
 
ASP.NET State management
Shivanand Arur
 
Event In JavaScript
ShahDhruv21
 
Javascript variables and datatypes
Varun C M
 
Javascript 101
Shlomi Komemi
 
Java Server Pages(jsp)
Manisha Keim
 
Css selectors
Parth Trivedi
 
Windowforms controls c#
prabhu rajendran
 
SQLITE Android
Sourabh Sahu
 
Introduction to HTML5
Gil Fink
 
Introduction to asp.net
SHADAB ALI
 
Introduction of Html/css/js
Knoldus Inc.
 
Angularjs PPT
Amit Baghel
 
ASP.NET MVC.
Ni
 
Introduction to JavaScript (1).ppt
MuhammadRehan856177
 
Lab #2: Introduction to Javascript
Walid Ashraf
 
ReactJS presentation.pptx
DivyanshGupta922023
 
Java Servlets
BG Java EE Course
 
android layouts
Deepa Rani
 

Similar to Asp.net state management (20)

DOC
State management in asp
Ibrahim MH
 
PDF
state management asp.net
Pratiksha Srivastava
 
PPT
StateManagement in ASP.Net.ppt
charusharma165
 
PPTX
Chapter 8 part1
application developer
 
PPTX
State Management.pptx
DrMonikaPatel2
 
PPTX
State management
teach4uin
 
DOCX
Managing states
Paneliya Prince
 
PPT
State management
Lalit Kale
 
PPTX
State management
Muhammad Amir
 
DOCX
State management
Iblesoft
 
PPT
Session and state management
Paneliya Prince
 
PPTX
State management in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
C# cookieless session id and application state
Malav Patel
 
PPTX
State management in ASP.net
baabtra.com - No. 1 supplier of quality freshers
 
PPSX
ASP.Net Presentation Part3
Neeraj Mathur
 
PPTX
ASP.NET Lecture 2
Julie Iskander
 
PPS
05 asp.net session07
Mani Chaubey
 
PPSX
05 asp.net session07
Vivek Singh Chandel
 
PPS
05 asp.net session07
Niit Care
 
State management in asp
Ibrahim MH
 
state management asp.net
Pratiksha Srivastava
 
StateManagement in ASP.Net.ppt
charusharma165
 
Chapter 8 part1
application developer
 
State Management.pptx
DrMonikaPatel2
 
State management
teach4uin
 
Managing states
Paneliya Prince
 
State management
Lalit Kale
 
State management
Muhammad Amir
 
State management
Iblesoft
 
Session and state management
Paneliya Prince
 
C# cookieless session id and application state
Malav Patel
 
ASP.Net Presentation Part3
Neeraj Mathur
 
ASP.NET Lecture 2
Julie Iskander
 
05 asp.net session07
Mani Chaubey
 
05 asp.net session07
Vivek Singh Chandel
 
05 asp.net session07
Niit Care
 
Ad

More from priya Nithya (19)

PPTX
Android Architecture.pptx
priya Nithya
 
PDF
Html server control - ASP. NET with c#
priya Nithya
 
PPTX
Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...
priya Nithya
 
PPTX
Asynchronous data transfer
priya Nithya
 
DOC
HTTP REQUEST RESPONSE OBJECT - WEB APPLICATION USING C# LAB
priya Nithya
 
DOCX
HTML SERVER CONTROL - ASP.NET WITH C#
priya Nithya
 
DOC
Android LAb - Creating an android app with Radio button
priya Nithya
 
PPTX
ASP.NET - Life cycle of asp
priya Nithya
 
DOC
Web application using c# Lab - Web Configuration file
priya Nithya
 
DOCX
Creation of simple application using - step by step
priya Nithya
 
PPSX
Adaptation of tcp window
priya Nithya
 
PPSX
Asp.net Overview
priya Nithya
 
PPSX
Key mechanism of mobile ip
priya Nithya
 
PPSX
Mobile ip overview
priya Nithya
 
PPSX
Features of mobile ip
priya Nithya
 
PDF
Creating a Name seperator Custom Control using C#
priya Nithya
 
PDF
How to Create Database component -Enterprise Application Using C# Lab
priya Nithya
 
PDF
Creating simple component
priya Nithya
 
PPT
Internet (i mcom)
priya Nithya
 
Android Architecture.pptx
priya Nithya
 
Html server control - ASP. NET with c#
priya Nithya
 
Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...
priya Nithya
 
Asynchronous data transfer
priya Nithya
 
HTTP REQUEST RESPONSE OBJECT - WEB APPLICATION USING C# LAB
priya Nithya
 
HTML SERVER CONTROL - ASP.NET WITH C#
priya Nithya
 
Android LAb - Creating an android app with Radio button
priya Nithya
 
ASP.NET - Life cycle of asp
priya Nithya
 
Web application using c# Lab - Web Configuration file
priya Nithya
 
Creation of simple application using - step by step
priya Nithya
 
Adaptation of tcp window
priya Nithya
 
Asp.net Overview
priya Nithya
 
Key mechanism of mobile ip
priya Nithya
 
Mobile ip overview
priya Nithya
 
Features of mobile ip
priya Nithya
 
Creating a Name seperator Custom Control using C#
priya Nithya
 
How to Create Database component -Enterprise Application Using C# Lab
priya Nithya
 
Creating simple component
priya Nithya
 
Internet (i mcom)
priya Nithya
 
Ad

Recently uploaded (20)

PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
epi editorial commitee meeting presentation
MIPLM
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
infertility, types,causes, impact, and management
Ritu480198
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Difference between write and update in odoo 18
Celine George
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Council of Chalcedon Re-Examined
Smiling Lungs
 

Asp.net state management

  • 2. ASP.NET - STATEMANAGEMENT  State Management is very important feature in ASP.NET.  State Management maintains & stores the information of any user till the end of the user session.  State management can be classified into two types.  Client side state management  Server side state management
  • 3. Client side state management Client side state management can be handled by  Hidden Fields  View State  Cookies  Query String  Control state
  • 4. Server side state Management Server side state management can be handled by  Session state  Application state
  • 5. Hidden Field control  Hidden Field is a control provided by ASP.NET, which is used to store small amount of data on the client  Hidden Field Control is not rendered to the browser and it is invisible on the browser. Syntax: <asp: HiddenField ID=“HiddenField1” runat=“server/>
  • 6. Hidden Field Control Example HiddenField.aspx
  • 7. HiddenField.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class HiddenField : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (HiddenField1.Value != null) { int val = Convert.ToInt32(HiddenField1.Value) + 1; HiddenField1.Value = val.ToString(); Label1.Text = "The Hidden Field Value is Incremented by 1 & the Current Value is : <B>" + val.ToString() + "</B>"; } } protected void Button1_Click(object sender, EventArgs e) { Label1.Visible = true; } }
  • 9. ViewState  View state is used to store user’s data, which is provided by Asp.net client side state management mechanism.  ViewState stores data in the generated HTML using hidden field , not on the server.  ViewState provides page level state management.  ie., as long as the user is on the current page, the state will be available; Once the user redirects to the next page , the current state will be lost.  ViewState can store any type of data & it will be enabled by default for all the serverside control by setting true to “EnableViewState” property.
  • 11. Viewstate.aspx.cs protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { if (ViewState["count"] != null) { int viewstatecount = Convert.ToInt32(ViewState["count"]) + 1; Label2.Text = viewstatecount.ToString(); ViewState["count"] = viewstatecount.ToString(); } else { ViewState["count"] = "1"; } } }
  • 12. protected void btn_Store_Click(object sender, EventArgs e) { ViewState["name"] = txtName.Text; txtName.Text = ""; Label2.Text=ViewState["count"].ToString(); } protected void btn_Display_Click(object sender, EventArgs e) { Label1.Text =ViewState["name"].ToString(); Label2.Text =ViewState["count"].ToString(); }
  • 15. QUERYSTRING  A Querystring is a collection of character input to a computer or a browser.  In Querystring , we can sent value to only desired page & the value will be temporarily stored.  Querystring increase the overall performance of web application.  When we pass content between webforms, the Querystring followed with a separating character (?).  The question mark(?) is, basically used for identifying data appearing after the separating symbol.
  • 17. QueryHome.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class QueryHome : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btn_submit_Click(object sender, EventArgs e) { Response.Redirect("QueryWelcome.aspx?firstname="+TextBox1.Text+ “lastname="+TextBox2.Text); } }
  • 18. QueryWelcome.aspx QueryWelcome.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class QueryWelcome : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { String firstname = Request.QueryString["firstname"]; String lastname= Request.QueryString["lastname"]; Label1.Text = "Welcome " + firstname + " " + lastname; } }
  • 21. COOKIES  Cookies are small piece of text which is stored on the client’s computer by the browser.  i.e. ., Cookies allows web applications to save user’s information to reuse , if needed. Cookies can be classified into 2 types 1. Persistence Cookie 2. Non-Persistence Cookie
  • 22. 1. Persistence Cookie  This types of cookies are permanently stored on user hard drive. Cookies which have an expiry date time are called persistence cookies.  This types of cookies stored user hard drive permanently till the date time we set. To create persistence cookie: Response.Cookies[“name”].Value = “Nithiyapriya”; Response.Cookies[“Nithiyapriya”].Expires = DateTime.Now.AddMinutes(2); (or) HttpCookie strname = new HttpCookie(“name”); strname.Value = “Nithiyapriya”; strname.Expires = DateTime.Now.AddMinutes(2); Response.Cookies.Add(strname); Here, the Cookie Expire time is set as 2 minutes; so that we can access the cookie values up to 2 minutes, after 2 minutes the cookies automatically expires.
  • 23. 2. Non-Persistence Cookie  This types of cookies are not permanently stored on user hard drive.  It stores the information up the user accessing the same browser.  When user close the browser the cookies will be automatically deleted. To create non-persistence cookie: Response.Cookies[“name”].Value = “Nithiyapriya”; (OR) HttpCookie strname = new HttpCookie(“name”); strname.Value = “Nithiyapriya”; Response.Cookies.Add(strname);
  • 25. Cookies.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Cookies : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["BackgroundColor"] != null) { ColorSelector.SelectedValue=Request.Cookies["BackgroundColor"].Value; BodyTag.Style["background-color"] = ColorSelector.SelectedValue; } } protected void ColorSelector_SelectedIndexChanged(object sender, EventArgs e) { BodyTag.Style["background-color"] = ColorSelector.SelectedValue; HttpCookie cookie = new HttpCookie("BackgroundColor"); cookie.Value = ColorSelector.SelectedValue; cookie.Expires = DateTime.Now.AddMinutes(2); Response.SetCookie(cookie); } }
  • 26. This background Color of this page retains, until 2 minutes, even after you closed the browser.
  • 27. Server side State Management Session State  The session is the important features of asp.net  Session state is used to store value for the particular period of time.  Session can store the client data on the sever separately for each user & it keeps value across multiple pages of website.  i.e. user can store some information in session in one page & it can be accessed on rest of all other pages by using session.  For example ,When a user login to any website with username & password, the username will be shown to all other pages.
  • 28. Syntax : Session[“session_name”] = “session value”; To Declare session in asp.net Session[“name”]=”Nithiyapriya”; Response.Redirect(“Welcomepage.aspx”); To Retrieve session value onWelcomepage string myvalue= Session[“name”].ToString(); Response.Write(“Name = ” + myvalue); Example: To set SessionTimeout add this code toWeb.config <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> <sessionState timeout="1"></sessionState> </system.web>
  • 29. Login.aspx Login.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { if (txtusername.Text == "admin" && txtpassword.Text == "admin") { Session["uname"] = txtusername.Text; Response.Redirect("Main.aspx"); } else { Label1.Text = "Wrong UserName/Password"; } } }
  • 30. Main.aspx Main.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Main : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Session["uname"] == null) { // Response.Redirect("Login.aspx"); Label1.Text = "Your Session is Expired"; } else { Label1.Text = "Welcome " + Session["uname"].ToString(); } } protected void btn_logout_Click(object sender, EventArgs e) { Session["uname"] = null; Response.Redirect("Login.aspx"); } }
  • 31. After 1 Minute of Idle this page will automatically signed out.
  • 32. Application State  Application state is server side state management mechanism.  Application state is used to store data on server & shared for all the users and it can be accessible anywhere in the application.  The application state is used same as session, but the difference is, the session state is specific for a single user ; where as an application state is common for all users.
  • 33. To Store value in application state Application[“name”] = “Nithiyapriya”; To get value from application state string str = Application[“name”].ToString();  Example:  Here we are going to calculate how many times a given page has been visited by various clients.
  • 35. ApplicationState.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class ApplicationState : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click1(object sender, EventArgs e) { int count = 0; if (Application["visit"] != null) { count = Convert.ToInt32(Application["visit"].ToString()); } count = count + 1; Application["visit"] = count; Label1.Text = "This page is been visited for <b> " + count.ToString() + " </b>times"; } }