Professional Documents
Culture Documents
Developing Web Applications Using Microsoft Visual Studio 2008
Developing Web Applications Using Microsoft Visual Studio 2008
Applications Using
Microsoft® Visual
Studio® 2008
Module 14: Managing State
• State Management
Login.aspx Login.aspx
Please enter your Please enter your
logon information: logon information:
First Name First Name
John John
Last Name Last Name
Chen Chen
Submit
Submit Web Server Submit
Submit Web Server
Greetings.aspx Greetings.aspx
I forget who
you are!!
Types of State Management
Database
Query strings
• In some cases, use database
support to maintain state on your • Information appended to the end
of a URL
Web site
Server-Side State Management
• Application state is a global storage mechanism accessible
from all pages in the Web application
• Session state is limited to the current browser session:
Values are preserved through the use of application and
session variables
Scalability
Web Server
Client Computer
Client Computer
Cookies
The Global.asax File
• Only one Global.asax file per Web application
[Visual C#]
protected void Application_Start(Object
sender,EventArgs e)
{
Application["NumberofVisitors"] = 0;
}
[Visual Basic]
Sub Application_Start(s As Object,e As EventArgs)
Application("NumberofVisitors") = 0
End Sub
[Visual C#]
[Visual Basic]
Session["BackColor"] = "blue";
Session("BackColor") = "blue"
Application.Lock();
Application.Lock()
Application["NumberOfVisitors"] =
Application("NumberOfVisitors") += 1
(int)Application["NumberOfVisitors"] + 1;
Application.UnLock()
Application.UnLock();
[Visual C#]
[Visual Basic]
bgColor = (string)Session["BackColor"];
bgColor = Session("BackColor")
visitorLabel.Text =
visitorLabel.Text = Application("NumberOfVisitors")
Application["NumberOfVisitors"].ToString();
Application and Session Variable Duration
• Session variables have a set duration after last access:
Default is 20 minutes
<configuration>
<system.web>
<sessionState timeout="10" />
</system.web>
</configuration>
Web farm
Session and
Application variables
-Or-
SQL
Session and
Application variables
Client
Saving Application and Session Variables in
a Database
<sessionState mode="SQLServer"
sqlConnectionString="data source=SQLServerName;
Integrated security=true" />
• Cookieless Sessions
[Visual C#]
[Visual C#]
HttpCookie getCookie = new HttpCookie("myCookie");
HttpCookie getCookie = new HttpCookie("myCookie");
DateTime now = DateTime.Now;
DateTime now = DateTime.Now;
getCookie.Values.Add("Time", now.ToString());
getCookie.Values.Add("Time", now.ToString());
getCookie.Values.Add("ForeColor", "White");
getCookie.Values.Add("ForeColor", "White");
getCookie.Values.Add("BackColor", "Blue");
getCookie.Values.Add("BackColor", "Blue"); To create a
getCookie.Expires = now.AddHours(1);
Response.Cookies.Add(getCookie); persistent cookie,
Response.Cookies.Add(getCookie); specify the
expiration time
[Visual Basic]
timeLabel.Text = myCookie.Values("Time")
timeLabel.ForeColor = System.Drawing.Color.FromName _
(myCookie.Values("ForeColor"))
timeLabel.BackColor = System.Drawing.Color.FromName _
(myCookie.Values("BackColor"))
Cookieless Sessions
• Each active session is identified and tracked by using a
session IDs
• Session IDs are communicated across client-server
requests by using an HTTP cookie or including it in the URL
• Cookieless sessions
Session ID information is encoded into URLs
http://server/(h44a1e55c0breu552yrecobl)/page.aspx
Logon information
Doctors Dentists
XML Files
Lab Review
Review Questions
• How do you set session variables?
• Best Practices
• Tools