Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Day 3

ASP.NET
1

Prepared by Arun Kumar E


Objectives
 State Management
 Client Side
 Server Side

Prepared by Arun Kumar E


2
State Management
 HTTP is a stateless protocol.
 Once the server serves any request from the user, it
cleans up all the resources used to serve that request.
 These resources include the objects created during that

Prepared by Arun Kumar E


request, the memory allocated during that request.
 If we have to track the users' information between page
visits and even on multiple visits of the same page, then
we need to use the State management techniques
provided by ASP.NET.
 State management is the process by which ASP.NET let
the developers maintain state and page information over
multiple request for the same or different pages. 3
State Management
 There are 2 types of State Management in ASP.NET

 Client-side State Management


 When we use client side state management, the state
related information will be stored on client side. This

Prepared by Arun Kumar E


information will travel back and forth with every request
and response.
 Server-side State Management
 Server side state management, in contrast to client side,
keeps all the information in user memory. The downside
of this is more memory usage on server and the benefit is
that users' confidential and sensitive information is
secure. 4
Client-side State Management
 A new instance of the Web page class is created each
time the page is posted to the server.
 As a result, all information associated with the page
and the controls on the page would be lost with each
round trip.

Prepared by Arun Kumar E


 Client-side state management allows you to store this
information either on a Web page or on a client
computer.
 The various options available to implement client-side
state management are:
 Hidden fields
 View state 5
 Cookies
 Query strings
Hidden Fields
 A hidden field is a control similar to a TextBox
control.
 A hidden field does not render in a Web browser. As
a result, a user cannot type anything in it.

Prepared by Arun Kumar E


 Hidden fields can be used to store information that
needs to be submitted along with the Web page, but
should not be displayed on the page.
 Hidden fields can also be used to pass session
information to and from forms, transparently.

String color = Request.Form["HiddenField1"]; 6


View State
 Each Web page and controls on the page have a
property called ViewState.
 This property is used to automatically save the
values of the Web page and each control on the Web

Prepared by Arun Kumar E


page prior to rendering the page.
 The view state is implemented with the help of a
hidden form field called _VIEWSTATE.
 This hidden form field is automatically created in
every Web page.

7
Contd…
 Storing information in view state:
 Information in the form fields on a Web page is
automatically added to the view state.
 The view state can be used to store some additional
information.
 For example, you can store a key-value pair, Counter=1,

Prepared by Arun Kumar E


in the view state by using the following code:
ViewState["Counter"] = 1;

 Retrieving information from view state:


 A value stored in view state can be retrieved by using
code.
 For example, you can retrieve the value of the key,
Counter, from view state by using the following code
snippet: 8
int counter;
counter = (int) ViewState["Counter"];
Cookies
 Cookies are:
 Used to store small pieces of information related to a user’s
computer such as its IP address, browser type, operating
system, and Web pages last visited.
 Sent to a client computer along with the page output.
 Types of cookies:

Prepared by Arun Kumar E


 Temporary cookies:
 Exist in the memory space of a browser.
 Also known as session cookies.
 Are useful for storing information required for only a short
time.
 Persistent cookies:
 Are saved as a text file in the file system of the client
computer.
 Are used when you want to store information for a longer
period. 9
Contd…
 To Create Persistent Cookies
Response.Cookies.Add(new HttpCookie("UserID",
txtUid.Text));
Response.Cookies[“UserID"].Expires =
DateTime.Now.AddSeconds(10);

Prepared by Arun Kumar E


 To Read Cookies
if (Request.Cookies[“UserID”] != null)
Label2.Text = Request.Cookies[“UserID"].Value;

10
Query String
 A Query String:
 Provides a simple way to pass information from one
page to another.
 Is the part of a URL that appears after the question

Prepared by Arun Kumar E


mark (?) character.
 You can pass data from one page to another page in
the form of a query string by using the
Response.Redirect method, as shown in the
following example:
Response.Redirect("BooksInfo.aspx?Category=
fiction&Publisher=Sams");
11
Server-side State Management
 There are situations where you need to store the
state information at the server-side.
 Server-side state management enables you to
manage application and session-related

Prepared by Arun Kumar E


information on the server.
 ASP.NET provides the following options to manage
state at the server side:
 Application state
 Session state

12
Application State
 ASP.NET provides application state as a means of
storing application-specific information such as
objects and variables.
 The information in the application state:

Prepared by Arun Kumar E


 Is stored in a key-value pair.
 Is used to maintain data consistency between
server round trips and between pages.
 Application state is created the first time a user
accesses any URL resource in an application.
 After an application state is created, the application-
specific information is stored in it. 13
Contd…
 Storing information in application state:
 You can add application-specific information to an
application state by creating variables and objects
and adding them to the application state.
 For example, you can create a variable called

Prepared by Arun Kumar E


MyVariable with the value Hello and store it in
the application state by using the following code
snippet:
Application ["MyVariable"] = "Hello";
 You can read the value of MyVariable by using the
following code snippet:
string val = (string) Application["MyVariable"];

14
Contd…
 Removing information from application state:
 You can remove an existing object or variable, such as
MyVariable from application state by using the
following code snippet:
Application.Remove(["MyVariable"]);
 You can also remove all the application state variables

Prepared by Arun Kumar E


and objects by using the following code snippet :
Application.RemoveAll();
 After an object is added to an application state, it
remains in the application state until:
 The application is shut down.
 The Global.asax file is modified.
 The item is explicitly removed from the application
15
state.
Contd…
 Handling application events:
 Application state has the following events that you
can capture to manipulate an ASP.NET Web
application:
 Application.Start

Prepared by Arun Kumar E


 Application.End
 Application.Error
 It is important to consider the following issues
while storing any value in an application state
variable:
 The number of variables and objects in an application
state should be minimum.
16
 Explicit synchronization methods need to be used to
avoid deadlocks and access violations.
Session State
 In ASP.NET, session state is used to store session-
specific information for a Web application.
 The scope of session state is limited to the current
browser session.

Prepared by Arun Kumar E


 Session state is structured as a key-value pair for
storing session-specific information that needs to be
maintained between server round trips and between
requests for pages.

17
Contd…
 Session state is not lost if the user revisits a Web
page by using the same browser window.
 However, session state can be lost in the following
ways:

Prepared by Arun Kumar E


 When the user closes and restarts the browser.
 When the user accesses the same Web page through a
different browser window.
 When the session times out because of inactivity.
 When the Session.Abandon() method is called
within the Web page code.
18
Contd…
 Session state has the following events that you can
capture to manipulate an ASP.NET Web application:
 Session.Start
 Session.End

Prepared by Arun Kumar E


 Session state can be configured through the
web.config file for the application.
 The web.config file allows you to set advanced
options such as the timeout and the session state
mode.

19
Contd…
The following code snippet shows some important
options that can be set for the <sessionState>
element:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

Prepared by Arun Kumar E


<system.web>
<sessionState
cookieless="UseCookies"
cookieName="ASP.NET_SessionId"
timeout="20"
mode="InProc" />
</system.web>
</configuration> 20

You might also like