5 - Managing State
5 - Managing State
Managing State
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology
State
• Web pages are stateless
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 2
Preserving State
• The state of a web application helps us to store the
runtime changes that are made to the web
application
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 3
Preserving State
• Hidden Fields
▫ Refers to a control that is not visible when a web
application is viewed in browser
▫ Contents of the control are sent in HTTP Form
Collection control to the server
• Cookies
▫ Refers to the text files that store data (like user ID)
▫ On page request, cookie is sent along with the page
▫ Web server then retrieves the information from the
cookie
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 4
Preserving State
• Query Strings
▫ Refers to the information strings added at the end of
a URL
▫ Not secure because information is exposed through
URL
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 5
Preserving State
• Session State
▫ Stores information specific to user session (duration
for which the user uses the website)
▫ Object of HttpSessionState class is used
• Application State
▫ Stores application data not frequently modified by
users
▫ Object of HttpApplicationState class is used
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 6
Page-Level state
• ViewState property is used to store the page-level state
of a web page
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 7
Page-Level state
• Ex:
ViewState[“Counter”] = 1;
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 8
protected void BtnIncrement_Click(object sender, EventArgs e)
{
int Count;
if (ViewState["Counter"] == null)
{
Count = 1;
}
else
{
Count = (int) ViewState["Counter"] + 1;
}
ViewState["Counter"] = Count;
}
Counter increments each time a
button is pressed. Counter value is
preserved even if the page is
reloaded, until the page is closed.
Query String
• Viewstate is tightly bound to a specific page
• If user navigates to another page, information is
lost
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 12
Query String
• Limitations
▫ Information is limited to simple strings
▫ Information is clearly visible to anyone
▫ Cannot place large amount of information in query
string as many browsers impose a limit on length of
the URL
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 13
\\QueryString_1.aspx
Response.Redirect(url);
}
\\QueryString_2.aspx
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 20
Cookies
• Import : System.Net
Cookie Object Cookie Name
• Retrieving Cookies
HttpCookies Cookie = Request.Cookies[“preferences”];
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 22
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["Preferences"];
if (cookie == null)
{
lblWelcome.Text = "<b>Unknown Customer</b>";
}
else
{
lblWelcome.Text = "<b>Cookie Found.</b><br><br>";
lblWelcome.Text += "Welcome, " + cookie["Name"];
}
}
// continue….
protected void cmdStore_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["Preferences"];
if (cookie == null)
{
cookie = new HttpCookie("Preferences");
}
cookie["Name"] = txtName.Text;
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);
}
Reload the page
Session State
• Session: A period of time in which a user interacts with a
web application
• Session state is a collection of data
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 31
Session State
• Session state is maintained by providing a unique
SessionID to the user, when the session begins
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 32
Session State Modes
• In-Process
▫ Refers to the mode in which the session state memory is kept within
ASP.NET process
▫ Used with web applications hosted on a single server
• Out-of-Process (StateServer)
▫ Refers to the mode in which there is a reliability of a separate
process that manages the state of all servers
• SQL Server
▫ Refers to the mode in which there is a reliability of the data within a
web application with the help of a database
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 33
Storing objects in Session State
• Create session variables
Session[“Name”] = txtName.Text;
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 34
Using Session State
• We can interact with session state using
System.Web.SessionState.HttpSessionState class
• HttpSessionState Members
▫ Count
Number of items in session collection
▫ IsCookieless
Identifies if session is tracked with cookies
Gets a value indicating whether the session ID is embedded in the
URL or stored in an HTTP cookie.
▫ IsNewSession
identifies whether the session is just created
▫ Mode
specifies how ASP.NET stores session state information
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 35
Using Session State
• HttpSessionState Members:
▫ SessionID
Provides a string with a unique session identifier
▫ TimeOut
Number of minutes that must elapse before the session will be
abandoned
▫ Abandon()
Cancels the current session immediately and releases all the
memory it occupied
▫ Clear()
Removes all session items
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 36
// Session_1.aspx
}
Configuring Session State
• Session state is configured through web.config file
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 41
Configuring Session State
• Timeout
▫ Specifies the number of minutes that ASP.NET waits,
before it abandons the session
<sessionState
timeout=“10”
/>
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 42
Configuring Session State
• Mode
▫ InProc
Instructs information to be stored in the same process
Best performance, but least durability
If server restarts, state information will be lost
Default mode
<sessionState mode=“InProc” />
▫ Off
Disables session state management for every page in
the application
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 43
Configuring Session State
• Mode
▫ StateServer (setting up Out-of-Process State Server)
With this setting, ASP.NET uses a separate Windows Service
for state management
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 44
//web.config
<sessionState
mode="StateServer"
stateConnectionString="tcpip=localhost:1349"
/>
Configuring Session State
• Mode
▫ SqlServer (Storing Session State in SQL Server)
This setting instructs ASP.NET to use SQL server database to store
session information
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 48
Application State
• Stores global objects that can be accessed by any client
• Uses System.Web.HttpApplicationState class
• Ex: Global counter that tracks how many times a given page has been
visited by various clients.
int count = 0;
if (Application["Visit"] != null)
{
count = Convert.ToInt32(Application["Visit"].ToString());
}
count = count + 1;
Application["Visit"] = count;
Label1.Text = "Total Visit = " + count.ToString();
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 49
Application State
• Items in application state never time out, they last until the application or server is
restarted.
• If two clients request a page at a same time, one request may be lost.
• Use Lock() and Unlock() to allow only one client to access the value at a time.
Application.Lock();
int count = 0;
if (Application["Visit"] != null)
{
count = Convert.ToInt32(Application["Visit"].ToString());
}
count = count + 1;
Application["Visit"] = count;
Application.UnLock();
Label1.Text = "Total Visit = " + count.ToString();
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 50
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 51
Run on two different clients
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology 52
End
Prof. Bhumika Patel Sarvajanik College of Eng. & Tech., Surat Dotnet Technology