SlideShare a Scribd company logo
1 of 33
Developing Web Applications Using ASP.NET
In this session, you will learn to:
Describe the ViewState and ControlState data models for Web
pages
Describe the Application and Session objects and explain how
state data is stored and retrieved in these objects
Describe various session-state data-storage strategies
Describe the Cache object and explain how you can use it to
store and manage state data
Objectives
Developing Web Applications Using ASP.NET
A new instance of the Web page class is created each time
the page is posted to the server.
In traditional Web programming, all information that is
associated with the page, along with the controls on the
page, would be lost with each roundtrip.
Microsoft ASP.NET framework includes several options to
help you preserve data on both a per-page basis and an
application-wide basis. These options can be broadly
divided into two categories:
Client-Based State Management Options
Server-Based State Management Options
ASP.NET State Management Overview
Developing Web Applications Using ASP.NET
Client-based options involve storing information either in the
page or on the client computer.
Some client-based state management options are:
View state
Control state
Hidden form fields
Cookies
Query strings
Client-Based State Management Options
Developing Web Applications Using ASP.NET
View State:
The ViewState property provides a dictionary object for
retaining values between multiple requests for the same page.
When the page is processed, the current state of the page and
controls is hashed into a string and saved in the page as a
hidden field.
When the page is posted back to the server, the page parses
the view-state string at page initialization and restores property
information in the page.
To specify the maximum size allowed in a single view-state
field, you can use the property,
System.Web.UI.Page.MaxPageStateFieldLength.
In addition to the contents of controls, the ViewState property
can also contain some additional information:
ViewState[“color”] = “Yellow” ;
Client-Based State Management Options (Contd.)
Developing Web Applications Using ASP.NET
Control State:
The ControlState property allows you to persist property
information that is specific to a control.
This property cannot be turned off at a page level as can the
ViewState property.
Hidden Form Fields:
ASP.NET provides the HtmlInputHidden control, which
offers hidden-field functionality.
A hidden field does not render visibly in the browser.
The content of a hidden field is sent in the HTTP form
collection along with the values of other controls.
Client-Based State Management Options (Contd.)
Developing Web Applications Using ASP.NET
Cookies:
Cookies are stored either in a text file on the client file system
or in memory in the client browser session.
They contain site-specific information that a server sends to
the client along with the page output.
When the browser requests a page, the client sends the
information in the cookie along with the request information.
The server can read the cookie and extract its value.
Query Strings:
Query string is a piece of information that is appended to the
end of a page URL.
You must submit the page by using an HTTP GET command to
ensure availability of these values.
Client-Based State Management Options (Contd.)
Developing Web Applications Using ASP.NET
Server-based options maintain state information on the
server.
Some server-based state management options are:
Application state
Session state
Server-Based State Management Options
Developing Web Applications Using ASP.NET
Application State:
Application state is an instance of the
System.Web.HttpApplicationState class.
It allows you to save values for each active Web application.
Application state is stored in a key/value dictionary that is
created during each request to a specific URL.
It is a global storage mechanism that is accessible from all
pages in the Web application.
It supports the following events:
Application.Start
Application.End
Application.Error
The handlers for the preceding events can be defined in the
Global.asax file.
Server-Based State Management Options (Contd.)
Developing Web Applications Using ASP.NET
Values can be saved in the Application state as:
Application[“Message”]=“Hello,world.”;
Values can be retrieved from the Application state as:
if (Application[“AppStartTime”] != null)
{
DateTime myAppStartTime = (DateTime)
Application[“AppStartTime”];
}
Server-Based State Management Options (Contd.)
Developing Web Applications Using ASP.NET
Session State:
Session state is an instance of the
System.Web.SessionState.HttpSessionState class.
It allows you to save values for each active Web application
session.
It is similar to application state, except that it is scoped to the
current browser session.
It supports the following events:
Session.Start
Session.End
The handlers for the preceding events can be defined in the
Global.asax file.
Server-Based State Management Options (Contd.)
Developing Web Applications Using ASP.NET
Values can be saved in the Session state as:
string name = “Jeff”;
Session[“Name”] = name;
Values can be retrieved from the Session state as:
if(Session[“Name”]!=null)
{
string name=(string)Session[“Name”];
}
Server-Based State Management Options (Contd.)
Developing Web Applications Using ASP.NET
Session state information can be stored in several locations.
Location can be configured by setting the mode attribute of
the SessionState element in Web.config file.
Three possible storage modes can be used:
InProc Mode
State Server Mode
SQL Server Mode
Strategies for Managing Session State Data
Developing Web Applications Using ASP.NET
InProc Mode:
This is the default mode.
Session state data is stored in memory on the Web server
within the process that is running the Web application.
This is the only mode that supports the Session.End event.
Session state information will be lost if application is restarted.
Session state cannot be shared between multiple servers in a
Web farm.
To enable InProc mode, the following markup can be added
within the <system.web> tags in the Web.config file:
<sessionState mode=“InProc”></sessionState>
Strategies for Managing Session State Data
Developing Web Applications Using ASP.NET
Following Diagram shows how Session state information is
managed in InProc mode, when Cookies are enabled.
Strategies for Managing Session State Data (Contd.)
Client Server
Request received
Create session object and
store data in it
Fetch session object identified
by cookie
Fetch data from session object
Receives first page
Request first page
Store cookie on client,
send along with first page
Second request (with cookie)
Send second page based
on data in Session object
Receives second page
Session ID Cookies
Developing Web Applications Using ASP.NET
State Server Mode:
Session state data is managed by a separate process called
the ASP.NET State Service.
Session state information will not be lost if application is
restarted.
A single session state can be shared between multiple servers
in a Web farm.
ASP.NET State Service does not start automatically.
To use ASP.NET State Service, make sure it is running on the
desired server.
Strategies for Managing Session State Data (Contd.)
Developing Web Applications Using ASP.NET
You need to configure each Web server in the Web farm to
connect to the ASP.NET state service by modifying the
Web.config file as:
<configuration>
<system.web>
<sessionState mode="StateServer“
stateConnectionString=
"tcpip=MyStateServer:42424"
cookieless="AutoDetect"
timeout="20"/>
</system.web>
</configuration>
Strategies for Managing Session State Data (Contd.)
Developing Web Applications Using ASP.NET
Strategies for Managing Session State Data (Contd.)
Status of ASP.NET State Service can be checked from the
Services window on the server on which the service is
running.
Developing Web Applications Using ASP.NET
SQL Server Mode:
Session state data is stored in Microsoft SQL Server database.
Session state information will not be lost, in case application is
restarted.
Session state can be shared between multiple servers in a
Web farm.
To use SQLServer mode, session state database must be
installed on an existing SQL Server.
Strategies for Managing Session State Data (Contd.)
Developing Web Applications Using ASP.NET
After database has been installed, you need to specify
SQLServer mode in Web.config file as:
<configuration>
<system.web>
<sessionState mode="SQLServer"
sqlConnectionString="
Integrated Security=SSPI;data
source=MySqlServer;" />
</system.web>
</configuration>
Strategies for Managing Session State Data (Contd.)
Developing Web Applications Using ASP.NET
An object can be stored in cache if it consumes a lot of
server resources during creation and is being used
frequently.
This caching system can be used to improve the response
time of an application.
This caching system automatically removes items when
system memory becomes scarce, in a process called
scavenging.
The Cache Object
Developing Web Applications Using ASP.NET
To cache an item, you can use any of the following methods:
Key name/Value pair method:
Add a value to the cache object by using the key name as indexer.
The following code caches the value in the Text property of a
TextBox control called txtExample:
Cache[“key"] = value;
Insert method:
Specify the key name as the first argument and the value being
cached as the second argument.
The following code performs the same task as the previous
example by using the Insert method of the Cache object:
Cache.Insert(“key", value);
How to: Store and Retrieve State Data in the Cache Object
Developing Web Applications Using ASP.NET
To retrieve values from the cache, Cache object can be
referenced using the key name as the indexer:
if (Cache[“key"] != null)
{
txtExample.Text = (string)Cache[“key"];
}
If you try to access an object from the cache that has been
removed because of scarcity of memory, the cache will
return a Null reference.
How to: Store and Retrieve State Data in the Cache Object (Contd.)
Developing Web Applications Using ASP.NET
Expiration time of a cached object can be specified during
the object addition into the cache.
Expiration time can be:
Absolute
Sliding
Absolute Expiration:
This method specifies a date and time when the object will be
removed.
To add an item to the cache with an absolute expiration of two
minutes, the following code snippet can be used:
Cache.Insert("CacheItem", "Cached Item Value",
null, DateTime.Now.AddMinutes(2),
System.Web.Caching.Cache.NoSlidingExpiration);
Controlling Expiration
Developing Web Applications Using ASP.NET
Sliding Expiration:
This method specifies a duration for which an item can lie
unused in the cache.
The caching system can scavenge the item if it has not been
used for a duration that exceeds this value.
To add an item to the cache with a sliding expiration of 10
minutes, the following code snippet can be used:
Cache.Insert("CacheItem", "Cached Item Value", null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
new TimeSpan(0, 10, 0));
Controlling Expiration
Developing Web Applications Using ASP.NET
Policy for removing objects from the cache can be
influenced by specifying CacheItemPriority value.
The caching system will scavenge low-priority items before
high-priority items when system memory becomes scarce.
To set the priority of ac cached object to high at the time of
creation, the following code snippet can be used:
Cache.Insert("CacheItem", "Cached Item Value",
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.High, null);
Cache Item Priority
Developing Web Applications Using ASP.NET
Cached items can become invalid because the source of the
data has changed.
Cached items may be dependent on files, directories, and
other cached items.
At the time of adding an item to the cache, you can specify
the object on which the cached item depends.
If any object (on which a cached item depends) changes,
the cached item is automatically removed from the cache.
Cache Dependencies
Developing Web Applications Using ASP.NET
You can add an item with a dependency to the cache by
using the dependencies parameter in the Cache.Insert
method, as:
Cache.Insert(“Key", value, new CacheDependency
Server.MapPath("~myConfig.xml")));
You can also add an item that depends on another cached
item to the cache, as:
string[] sDependencies = new string[1];
sDependencies[0] = “key_original";
CacheDependency dependency = new
CacheDependency(null, sDependencies);
Cache.Insert(“key_dependent", "This item depends
on OriginalItem", dependency);
How to: Define Dependencies Between Cached Items
Developing Web Applications Using ASP.NET
Cached data can be deleted by:
Setting expiration policies that determine the total amount of
time the item remains in the cache.
Setting expiration policies that are based on the amount of
time that must pass following the previous time the item was
accessed.
Specifying files, directories, or keys that the item is dependent
on. The item is removed from the cache when those
dependencies change.
Removing items from the cache by using the Cache.Remove
method as:
Cache.Remove("MyData1");
How to: Delete Cached Data
Developing Web Applications Using ASP.NET
How to: Implement Deletion Notifications in Cached Data
CacheItemRemovedCallback delegate defines the
signature to use while writing the event handlers to respond
when an item is deleted from the cache.
CacheItemRemovedReason enumeration can be used to
make event handlers dependent upon the reason the item is
deleted.
Developing Web Applications Using ASP.NET
How to: Implement Deletion Notifications in Cached Data (Contd.)
To notify an application when an item is deleted from the
cache:
Create a local variable that raises the event for the
CacheItemRemovedCallback delegate as:
private static CacheItemRemovedCallback
onRemove = null;
Create an event handler to respond when the item is removed
from the cache as:
static bool itemRemoved = false;
static CacheItemRemovedReason reason;
public void RemovedCallback(string key, object
value, CacheItemRemovedReason
callbackreason)
{ itemRemoved = true;
reason = callbackreason;
}
Developing Web Applications Using ASP.NET
How to: Implement Deletion Notifications in Cached Data (Contd.)
Create an instance of the CacheItemRemovedCallback
delegate that calls the event handler as:
onRemove = new
CacheItemRemovedCallback(this.RemovedCallback);
Add the item to the cache by using the Cache.Insert
method as:
Cache.Insert("MyData1", Source, null,
DateTime.Now.AddMinutes(2),
NoSlidingExpiration,
CacheItemPriority.High, onRemove);
Developing Web Applications Using ASP.NET
In this session, you learned that:
ViewState is the mechanism for preserving the contents and
state of the controls on a Web page during a round trip.
ASP.NET enables controls to preserve their ControlState even
when ViewState is disabled.
The Application and Session objects enable you to cache
information for use by any page in your ASP.NET application.
ASP.NET maintains a single Application object for each
application on your Web server.
An ASP.NET Session object is an object that represents a
user’s visit to your application.
Summary
Developing Web Applications Using ASP.NET
The possible storage modes for storing session state
information are:
InProc
StateServer
SQLServer
ASP.NET has a powerful caching system that you can use to
improve the response time of your application.
The ASP.NET caching system automatically removes items
from the cache when system memory becomes scarce.
Summary (Contd.)

More Related Content

What's hot

Databind in asp.net
Databind in asp.netDatabind in asp.net
Databind in asp.netSireesh K
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08Niit Care
 
Session viii(state mngtserver)
Session viii(state mngtserver)Session viii(state mngtserver)
Session viii(state mngtserver)Shrijan Tiwari
 
SPCA2013 - SharePoint Insanity Demystified
SPCA2013 - SharePoint Insanity DemystifiedSPCA2013 - SharePoint Insanity Demystified
SPCA2013 - SharePoint Insanity DemystifiedNCCOMMS
 
Pre Install Databases
Pre Install DatabasesPre Install Databases
Pre Install DatabasesLiquidHub
 
Step by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView TutorialsStep by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView TutorialsNilesh kumar Jadav
 
Oracle Endeca Developer's Guide
Oracle Endeca Developer's GuideOracle Endeca Developer's Guide
Oracle Endeca Developer's GuideKeyur Shah
 
Drupal as a data server
Drupal as a data serverDrupal as a data server
Drupal as a data serverJay Friendly
 
Search engine optimization (seo) from Endeca & ATG
Search engine optimization (seo) from Endeca & ATGSearch engine optimization (seo) from Endeca & ATG
Search engine optimization (seo) from Endeca & ATGVignesh sitaraman
 
Web application-for-financial-and-economic-data-analysis3
Web application-for-financial-and-economic-data-analysis3Web application-for-financial-and-economic-data-analysis3
Web application-for-financial-and-economic-data-analysis3Mike Taylor
 
SQL Server Reporting Services 2008
SQL Server Reporting Services 2008SQL Server Reporting Services 2008
SQL Server Reporting Services 2008VishalJharwade
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for AndroidJakir Hossain
 
Oracle Endeca Commerce - Installation Guide
Oracle Endeca Commerce - Installation GuideOracle Endeca Commerce - Installation Guide
Oracle Endeca Commerce - Installation GuideKeyur Shah
 
SharePoint 2007 Presentation
SharePoint 2007 PresentationSharePoint 2007 Presentation
SharePoint 2007 PresentationAjay Jain
 

What's hot (18)

Databind in asp.net
Databind in asp.netDatabind in asp.net
Databind in asp.net
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08
 
Session viii(state mngtserver)
Session viii(state mngtserver)Session viii(state mngtserver)
Session viii(state mngtserver)
 
SPCA2013 - SharePoint Insanity Demystified
SPCA2013 - SharePoint Insanity DemystifiedSPCA2013 - SharePoint Insanity Demystified
SPCA2013 - SharePoint Insanity Demystified
 
Pre Install Databases
Pre Install DatabasesPre Install Databases
Pre Install Databases
 
Step by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView TutorialsStep by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView Tutorials
 
Oracle Endeca Developer's Guide
Oracle Endeca Developer's GuideOracle Endeca Developer's Guide
Oracle Endeca Developer's Guide
 
GRID VIEW PPT
GRID VIEW PPTGRID VIEW PPT
GRID VIEW PPT
 
Drupal as a data server
Drupal as a data serverDrupal as a data server
Drupal as a data server
 
Search engine optimization (seo) from Endeca & ATG
Search engine optimization (seo) from Endeca & ATGSearch engine optimization (seo) from Endeca & ATG
Search engine optimization (seo) from Endeca & ATG
 
Web application-for-financial-and-economic-data-analysis3
Web application-for-financial-and-economic-data-analysis3Web application-for-financial-and-economic-data-analysis3
Web application-for-financial-and-economic-data-analysis3
 
Chapter12 (1)
Chapter12 (1)Chapter12 (1)
Chapter12 (1)
 
SQL Server Reporting Services 2008
SQL Server Reporting Services 2008SQL Server Reporting Services 2008
SQL Server Reporting Services 2008
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for Android
 
CAD Report
CAD ReportCAD Report
CAD Report
 
Oracle Endeca Commerce - Installation Guide
Oracle Endeca Commerce - Installation GuideOracle Endeca Commerce - Installation Guide
Oracle Endeca Commerce - Installation Guide
 
SharePoint 2007 Presentation
SharePoint 2007 PresentationSharePoint 2007 Presentation
SharePoint 2007 Presentation
 
Grid Vew Control VB
Grid Vew Control VBGrid Vew Control VB
Grid Vew Control VB
 

Viewers also liked

07 intel v_tune_session_10
07 intel v_tune_session_1007 intel v_tune_session_10
07 intel v_tune_session_10Vivek chan
 
12 asp.net session17
12 asp.net session1712 asp.net session17
12 asp.net session17Vivek chan
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04Vivek chan
 
14 asp.net session20
14 asp.net session2014 asp.net session20
14 asp.net session20Vivek chan
 
Vivek Chan | Technology Consultant
Vivek Chan | Technology Consultant Vivek Chan | Technology Consultant
Vivek Chan | Technology Consultant Vivek chan
 
Net framework session01
Net framework session01Net framework session01
Net framework session01Vivek chan
 
08 asp.net session11
08 asp.net session1108 asp.net session11
08 asp.net session11Vivek chan
 
16 asp.net session23
16 asp.net session2316 asp.net session23
16 asp.net session23Vivek chan
 
Vivek Chan | Technology Consultant
Vivek Chan | Technology Consultant Vivek Chan | Technology Consultant
Vivek Chan | Technology Consultant Vivek chan
 
09 intel v_tune_session_13
09 intel v_tune_session_1309 intel v_tune_session_13
09 intel v_tune_session_13Vivek chan
 
Wireless Communication via Mobile Phone Using DTMF
Wireless Communication via Mobile Phone Using DTMF Wireless Communication via Mobile Phone Using DTMF
Wireless Communication via Mobile Phone Using DTMF Vivek chan
 
Net framework session02
Net framework session02Net framework session02
Net framework session02Vivek chan
 
Series Parallel Circuit presentation for schools and kids
Series Parallel Circuit presentation for schools and kidsSeries Parallel Circuit presentation for schools and kids
Series Parallel Circuit presentation for schools and kidsVivek chan
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
CyberLab TCP/IP and IP Addressing & Subnetting
CyberLab TCP/IP and IP Addressing & SubnettingCyberLab TCP/IP and IP Addressing & Subnetting
CyberLab TCP/IP and IP Addressing & SubnettingVivek chan
 

Viewers also liked (15)

07 intel v_tune_session_10
07 intel v_tune_session_1007 intel v_tune_session_10
07 intel v_tune_session_10
 
12 asp.net session17
12 asp.net session1712 asp.net session17
12 asp.net session17
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04
 
14 asp.net session20
14 asp.net session2014 asp.net session20
14 asp.net session20
 
Vivek Chan | Technology Consultant
Vivek Chan | Technology Consultant Vivek Chan | Technology Consultant
Vivek Chan | Technology Consultant
 
Net framework session01
Net framework session01Net framework session01
Net framework session01
 
08 asp.net session11
08 asp.net session1108 asp.net session11
08 asp.net session11
 
16 asp.net session23
16 asp.net session2316 asp.net session23
16 asp.net session23
 
Vivek Chan | Technology Consultant
Vivek Chan | Technology Consultant Vivek Chan | Technology Consultant
Vivek Chan | Technology Consultant
 
09 intel v_tune_session_13
09 intel v_tune_session_1309 intel v_tune_session_13
09 intel v_tune_session_13
 
Wireless Communication via Mobile Phone Using DTMF
Wireless Communication via Mobile Phone Using DTMF Wireless Communication via Mobile Phone Using DTMF
Wireless Communication via Mobile Phone Using DTMF
 
Net framework session02
Net framework session02Net framework session02
Net framework session02
 
Series Parallel Circuit presentation for schools and kids
Series Parallel Circuit presentation for schools and kidsSeries Parallel Circuit presentation for schools and kids
Series Parallel Circuit presentation for schools and kids
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
CyberLab TCP/IP and IP Addressing & Subnetting
CyberLab TCP/IP and IP Addressing & SubnettingCyberLab TCP/IP and IP Addressing & Subnetting
CyberLab TCP/IP and IP Addressing & Subnetting
 

Similar to 05 asp.net session07

05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07Niit Care
 
State management
State managementState management
State managementIblesoft
 
StateManagement in ASP.Net.ppt
StateManagement in ASP.Net.pptStateManagement in ASP.Net.ppt
StateManagement in ASP.Net.pptcharusharma165
 
State management
State managementState management
State managementIblesoft
 
13 asp.net session19
13 asp.net session1913 asp.net session19
13 asp.net session19Vivek chan
 
C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questionsAkhil Mittal
 
11 asp.net session16
11 asp.net session1611 asp.net session16
11 asp.net session16Vivek chan
 
Programming web application
Programming web applicationProgramming web application
Programming web applicationaspnet123
 
The complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide showThe complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide showSubhas Malik
 
Parallelminds.asp.net with sp
Parallelminds.asp.net with spParallelminds.asp.net with sp
Parallelminds.asp.net with spparallelminder
 
Ch 04 asp.net application
Ch 04 asp.net application Ch 04 asp.net application
Ch 04 asp.net application Madhuri Kavade
 
ASP.Net Presentation Part1
ASP.Net Presentation Part1ASP.Net Presentation Part1
ASP.Net Presentation Part1Neeraj Mathur
 

Similar to 05 asp.net session07 (20)

05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07
 
Asp.net
Asp.netAsp.net
Asp.net
 
State management
State managementState management
State management
 
StateManagement in ASP.Net.ppt
StateManagement in ASP.Net.pptStateManagement in ASP.Net.ppt
StateManagement in ASP.Net.ppt
 
ASP.NET Lecture 2
ASP.NET Lecture 2ASP.NET Lecture 2
ASP.NET Lecture 2
 
State management
State managementState management
State management
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
Chapter 8 part2
Chapter 8   part2Chapter 8   part2
Chapter 8 part2
 
13 asp.net session19
13 asp.net session1913 asp.net session19
13 asp.net session19
 
C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questions
 
11 asp.net session16
11 asp.net session1611 asp.net session16
11 asp.net session16
 
Programming web application
Programming web applicationProgramming web application
Programming web application
 
The complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide showThe complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide show
 
Parallelminds.asp.net with sp
Parallelminds.asp.net with spParallelminds.asp.net with sp
Parallelminds.asp.net with sp
 
Ch 04 asp.net application
Ch 04 asp.net application Ch 04 asp.net application
Ch 04 asp.net application
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
NET_Training.pptx
NET_Training.pptxNET_Training.pptx
NET_Training.pptx
 
ASP.Net Presentation Part1
ASP.Net Presentation Part1ASP.Net Presentation Part1
ASP.Net Presentation Part1
 
Ch05 state management
Ch05 state managementCh05 state management
Ch05 state management
 

More from Vivek chan

Deceptive Marketing.pdf
Deceptive Marketing.pdfDeceptive Marketing.pdf
Deceptive Marketing.pdfVivek chan
 
brain controled wheel chair.pdf
brain controled wheel chair.pdfbrain controled wheel chair.pdf
brain controled wheel chair.pdfVivek chan
 
Mechanism of fullerene synthesis in the ARC REACTOR (Vivek Chan 2013)
Mechanism of fullerene synthesis in the ARC REACTOR (Vivek Chan 2013)Mechanism of fullerene synthesis in the ARC REACTOR (Vivek Chan 2013)
Mechanism of fullerene synthesis in the ARC REACTOR (Vivek Chan 2013)Vivek chan
 
Manav dharma shashtra tatha shashan paddati munshiram jigyasu
Manav dharma shashtra tatha shashan paddati   munshiram jigyasuManav dharma shashtra tatha shashan paddati   munshiram jigyasu
Manav dharma shashtra tatha shashan paddati munshiram jigyasuVivek chan
 
Self driving and connected cars fooling sensors and tracking drivers
Self driving and connected cars fooling sensors and tracking driversSelf driving and connected cars fooling sensors and tracking drivers
Self driving and connected cars fooling sensors and tracking driversVivek chan
 
EEG Acquisition Device to Control Wheelchair Using Thoughts
EEG Acquisition Device to Control Wheelchair Using ThoughtsEEG Acquisition Device to Control Wheelchair Using Thoughts
EEG Acquisition Device to Control Wheelchair Using ThoughtsVivek chan
 
Full Shri Ramcharitmanas in Hindi Complete With Meaning (Ramayana)
Full Shri Ramcharitmanas in Hindi Complete With Meaning (Ramayana)Full Shri Ramcharitmanas in Hindi Complete With Meaning (Ramayana)
Full Shri Ramcharitmanas in Hindi Complete With Meaning (Ramayana)Vivek chan
 
Net framework session03
Net framework session03Net framework session03
Net framework session03Vivek chan
 
04 intel v_tune_session_05
04 intel v_tune_session_0504 intel v_tune_session_05
04 intel v_tune_session_05Vivek chan
 
03 intel v_tune_session_04
03 intel v_tune_session_0403 intel v_tune_session_04
03 intel v_tune_session_04Vivek chan
 
02 intel v_tune_session_02
02 intel v_tune_session_0202 intel v_tune_session_02
02 intel v_tune_session_02Vivek chan
 
01 intel v_tune_session_01
01 intel v_tune_session_0101 intel v_tune_session_01
01 intel v_tune_session_01Vivek chan
 
02 asp.net session02
02 asp.net session0202 asp.net session02
02 asp.net session02Vivek chan
 
01 asp.net session01
01 asp.net session0101 asp.net session01
01 asp.net session01Vivek chan
 
15 asp.net session22
15 asp.net session2215 asp.net session22
15 asp.net session22Vivek chan
 
10 asp.net session14
10 asp.net session1410 asp.net session14
10 asp.net session14Vivek chan
 
09 asp.net session13
09 asp.net session1309 asp.net session13
09 asp.net session13Vivek chan
 
07 asp.net session10
07 asp.net session1007 asp.net session10
07 asp.net session10Vivek chan
 

More from Vivek chan (18)

Deceptive Marketing.pdf
Deceptive Marketing.pdfDeceptive Marketing.pdf
Deceptive Marketing.pdf
 
brain controled wheel chair.pdf
brain controled wheel chair.pdfbrain controled wheel chair.pdf
brain controled wheel chair.pdf
 
Mechanism of fullerene synthesis in the ARC REACTOR (Vivek Chan 2013)
Mechanism of fullerene synthesis in the ARC REACTOR (Vivek Chan 2013)Mechanism of fullerene synthesis in the ARC REACTOR (Vivek Chan 2013)
Mechanism of fullerene synthesis in the ARC REACTOR (Vivek Chan 2013)
 
Manav dharma shashtra tatha shashan paddati munshiram jigyasu
Manav dharma shashtra tatha shashan paddati   munshiram jigyasuManav dharma shashtra tatha shashan paddati   munshiram jigyasu
Manav dharma shashtra tatha shashan paddati munshiram jigyasu
 
Self driving and connected cars fooling sensors and tracking drivers
Self driving and connected cars fooling sensors and tracking driversSelf driving and connected cars fooling sensors and tracking drivers
Self driving and connected cars fooling sensors and tracking drivers
 
EEG Acquisition Device to Control Wheelchair Using Thoughts
EEG Acquisition Device to Control Wheelchair Using ThoughtsEEG Acquisition Device to Control Wheelchair Using Thoughts
EEG Acquisition Device to Control Wheelchair Using Thoughts
 
Full Shri Ramcharitmanas in Hindi Complete With Meaning (Ramayana)
Full Shri Ramcharitmanas in Hindi Complete With Meaning (Ramayana)Full Shri Ramcharitmanas in Hindi Complete With Meaning (Ramayana)
Full Shri Ramcharitmanas in Hindi Complete With Meaning (Ramayana)
 
Net framework session03
Net framework session03Net framework session03
Net framework session03
 
04 intel v_tune_session_05
04 intel v_tune_session_0504 intel v_tune_session_05
04 intel v_tune_session_05
 
03 intel v_tune_session_04
03 intel v_tune_session_0403 intel v_tune_session_04
03 intel v_tune_session_04
 
02 intel v_tune_session_02
02 intel v_tune_session_0202 intel v_tune_session_02
02 intel v_tune_session_02
 
01 intel v_tune_session_01
01 intel v_tune_session_0101 intel v_tune_session_01
01 intel v_tune_session_01
 
02 asp.net session02
02 asp.net session0202 asp.net session02
02 asp.net session02
 
01 asp.net session01
01 asp.net session0101 asp.net session01
01 asp.net session01
 
15 asp.net session22
15 asp.net session2215 asp.net session22
15 asp.net session22
 
10 asp.net session14
10 asp.net session1410 asp.net session14
10 asp.net session14
 
09 asp.net session13
09 asp.net session1309 asp.net session13
09 asp.net session13
 
07 asp.net session10
07 asp.net session1007 asp.net session10
07 asp.net session10
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 

05 asp.net session07

  • 1. Developing Web Applications Using ASP.NET In this session, you will learn to: Describe the ViewState and ControlState data models for Web pages Describe the Application and Session objects and explain how state data is stored and retrieved in these objects Describe various session-state data-storage strategies Describe the Cache object and explain how you can use it to store and manage state data Objectives
  • 2. Developing Web Applications Using ASP.NET A new instance of the Web page class is created each time the page is posted to the server. In traditional Web programming, all information that is associated with the page, along with the controls on the page, would be lost with each roundtrip. Microsoft ASP.NET framework includes several options to help you preserve data on both a per-page basis and an application-wide basis. These options can be broadly divided into two categories: Client-Based State Management Options Server-Based State Management Options ASP.NET State Management Overview
  • 3. Developing Web Applications Using ASP.NET Client-based options involve storing information either in the page or on the client computer. Some client-based state management options are: View state Control state Hidden form fields Cookies Query strings Client-Based State Management Options
  • 4. Developing Web Applications Using ASP.NET View State: The ViewState property provides a dictionary object for retaining values between multiple requests for the same page. When the page is processed, the current state of the page and controls is hashed into a string and saved in the page as a hidden field. When the page is posted back to the server, the page parses the view-state string at page initialization and restores property information in the page. To specify the maximum size allowed in a single view-state field, you can use the property, System.Web.UI.Page.MaxPageStateFieldLength. In addition to the contents of controls, the ViewState property can also contain some additional information: ViewState[“color”] = “Yellow” ; Client-Based State Management Options (Contd.)
  • 5. Developing Web Applications Using ASP.NET Control State: The ControlState property allows you to persist property information that is specific to a control. This property cannot be turned off at a page level as can the ViewState property. Hidden Form Fields: ASP.NET provides the HtmlInputHidden control, which offers hidden-field functionality. A hidden field does not render visibly in the browser. The content of a hidden field is sent in the HTTP form collection along with the values of other controls. Client-Based State Management Options (Contd.)
  • 6. Developing Web Applications Using ASP.NET Cookies: Cookies are stored either in a text file on the client file system or in memory in the client browser session. They contain site-specific information that a server sends to the client along with the page output. When the browser requests a page, the client sends the information in the cookie along with the request information. The server can read the cookie and extract its value. Query Strings: Query string is a piece of information that is appended to the end of a page URL. You must submit the page by using an HTTP GET command to ensure availability of these values. Client-Based State Management Options (Contd.)
  • 7. Developing Web Applications Using ASP.NET Server-based options maintain state information on the server. Some server-based state management options are: Application state Session state Server-Based State Management Options
  • 8. Developing Web Applications Using ASP.NET Application State: Application state is an instance of the System.Web.HttpApplicationState class. It allows you to save values for each active Web application. Application state is stored in a key/value dictionary that is created during each request to a specific URL. It is a global storage mechanism that is accessible from all pages in the Web application. It supports the following events: Application.Start Application.End Application.Error The handlers for the preceding events can be defined in the Global.asax file. Server-Based State Management Options (Contd.)
  • 9. Developing Web Applications Using ASP.NET Values can be saved in the Application state as: Application[“Message”]=“Hello,world.”; Values can be retrieved from the Application state as: if (Application[“AppStartTime”] != null) { DateTime myAppStartTime = (DateTime) Application[“AppStartTime”]; } Server-Based State Management Options (Contd.)
  • 10. Developing Web Applications Using ASP.NET Session State: Session state is an instance of the System.Web.SessionState.HttpSessionState class. It allows you to save values for each active Web application session. It is similar to application state, except that it is scoped to the current browser session. It supports the following events: Session.Start Session.End The handlers for the preceding events can be defined in the Global.asax file. Server-Based State Management Options (Contd.)
  • 11. Developing Web Applications Using ASP.NET Values can be saved in the Session state as: string name = “Jeff”; Session[“Name”] = name; Values can be retrieved from the Session state as: if(Session[“Name”]!=null) { string name=(string)Session[“Name”]; } Server-Based State Management Options (Contd.)
  • 12. Developing Web Applications Using ASP.NET Session state information can be stored in several locations. Location can be configured by setting the mode attribute of the SessionState element in Web.config file. Three possible storage modes can be used: InProc Mode State Server Mode SQL Server Mode Strategies for Managing Session State Data
  • 13. Developing Web Applications Using ASP.NET InProc Mode: This is the default mode. Session state data is stored in memory on the Web server within the process that is running the Web application. This is the only mode that supports the Session.End event. Session state information will be lost if application is restarted. Session state cannot be shared between multiple servers in a Web farm. To enable InProc mode, the following markup can be added within the <system.web> tags in the Web.config file: <sessionState mode=“InProc”></sessionState> Strategies for Managing Session State Data
  • 14. Developing Web Applications Using ASP.NET Following Diagram shows how Session state information is managed in InProc mode, when Cookies are enabled. Strategies for Managing Session State Data (Contd.) Client Server Request received Create session object and store data in it Fetch session object identified by cookie Fetch data from session object Receives first page Request first page Store cookie on client, send along with first page Second request (with cookie) Send second page based on data in Session object Receives second page Session ID Cookies
  • 15. Developing Web Applications Using ASP.NET State Server Mode: Session state data is managed by a separate process called the ASP.NET State Service. Session state information will not be lost if application is restarted. A single session state can be shared between multiple servers in a Web farm. ASP.NET State Service does not start automatically. To use ASP.NET State Service, make sure it is running on the desired server. Strategies for Managing Session State Data (Contd.)
  • 16. Developing Web Applications Using ASP.NET You need to configure each Web server in the Web farm to connect to the ASP.NET state service by modifying the Web.config file as: <configuration> <system.web> <sessionState mode="StateServer“ stateConnectionString= "tcpip=MyStateServer:42424" cookieless="AutoDetect" timeout="20"/> </system.web> </configuration> Strategies for Managing Session State Data (Contd.)
  • 17. Developing Web Applications Using ASP.NET Strategies for Managing Session State Data (Contd.) Status of ASP.NET State Service can be checked from the Services window on the server on which the service is running.
  • 18. Developing Web Applications Using ASP.NET SQL Server Mode: Session state data is stored in Microsoft SQL Server database. Session state information will not be lost, in case application is restarted. Session state can be shared between multiple servers in a Web farm. To use SQLServer mode, session state database must be installed on an existing SQL Server. Strategies for Managing Session State Data (Contd.)
  • 19. Developing Web Applications Using ASP.NET After database has been installed, you need to specify SQLServer mode in Web.config file as: <configuration> <system.web> <sessionState mode="SQLServer" sqlConnectionString=" Integrated Security=SSPI;data source=MySqlServer;" /> </system.web> </configuration> Strategies for Managing Session State Data (Contd.)
  • 20. Developing Web Applications Using ASP.NET An object can be stored in cache if it consumes a lot of server resources during creation and is being used frequently. This caching system can be used to improve the response time of an application. This caching system automatically removes items when system memory becomes scarce, in a process called scavenging. The Cache Object
  • 21. Developing Web Applications Using ASP.NET To cache an item, you can use any of the following methods: Key name/Value pair method: Add a value to the cache object by using the key name as indexer. The following code caches the value in the Text property of a TextBox control called txtExample: Cache[“key"] = value; Insert method: Specify the key name as the first argument and the value being cached as the second argument. The following code performs the same task as the previous example by using the Insert method of the Cache object: Cache.Insert(“key", value); How to: Store and Retrieve State Data in the Cache Object
  • 22. Developing Web Applications Using ASP.NET To retrieve values from the cache, Cache object can be referenced using the key name as the indexer: if (Cache[“key"] != null) { txtExample.Text = (string)Cache[“key"]; } If you try to access an object from the cache that has been removed because of scarcity of memory, the cache will return a Null reference. How to: Store and Retrieve State Data in the Cache Object (Contd.)
  • 23. Developing Web Applications Using ASP.NET Expiration time of a cached object can be specified during the object addition into the cache. Expiration time can be: Absolute Sliding Absolute Expiration: This method specifies a date and time when the object will be removed. To add an item to the cache with an absolute expiration of two minutes, the following code snippet can be used: Cache.Insert("CacheItem", "Cached Item Value", null, DateTime.Now.AddMinutes(2), System.Web.Caching.Cache.NoSlidingExpiration); Controlling Expiration
  • 24. Developing Web Applications Using ASP.NET Sliding Expiration: This method specifies a duration for which an item can lie unused in the cache. The caching system can scavenge the item if it has not been used for a duration that exceeds this value. To add an item to the cache with a sliding expiration of 10 minutes, the following code snippet can be used: Cache.Insert("CacheItem", "Cached Item Value", null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0)); Controlling Expiration
  • 25. Developing Web Applications Using ASP.NET Policy for removing objects from the cache can be influenced by specifying CacheItemPriority value. The caching system will scavenge low-priority items before high-priority items when system memory becomes scarce. To set the priority of ac cached object to high at the time of creation, the following code snippet can be used: Cache.Insert("CacheItem", "Cached Item Value", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null); Cache Item Priority
  • 26. Developing Web Applications Using ASP.NET Cached items can become invalid because the source of the data has changed. Cached items may be dependent on files, directories, and other cached items. At the time of adding an item to the cache, you can specify the object on which the cached item depends. If any object (on which a cached item depends) changes, the cached item is automatically removed from the cache. Cache Dependencies
  • 27. Developing Web Applications Using ASP.NET You can add an item with a dependency to the cache by using the dependencies parameter in the Cache.Insert method, as: Cache.Insert(“Key", value, new CacheDependency Server.MapPath("~myConfig.xml"))); You can also add an item that depends on another cached item to the cache, as: string[] sDependencies = new string[1]; sDependencies[0] = “key_original"; CacheDependency dependency = new CacheDependency(null, sDependencies); Cache.Insert(“key_dependent", "This item depends on OriginalItem", dependency); How to: Define Dependencies Between Cached Items
  • 28. Developing Web Applications Using ASP.NET Cached data can be deleted by: Setting expiration policies that determine the total amount of time the item remains in the cache. Setting expiration policies that are based on the amount of time that must pass following the previous time the item was accessed. Specifying files, directories, or keys that the item is dependent on. The item is removed from the cache when those dependencies change. Removing items from the cache by using the Cache.Remove method as: Cache.Remove("MyData1"); How to: Delete Cached Data
  • 29. Developing Web Applications Using ASP.NET How to: Implement Deletion Notifications in Cached Data CacheItemRemovedCallback delegate defines the signature to use while writing the event handlers to respond when an item is deleted from the cache. CacheItemRemovedReason enumeration can be used to make event handlers dependent upon the reason the item is deleted.
  • 30. Developing Web Applications Using ASP.NET How to: Implement Deletion Notifications in Cached Data (Contd.) To notify an application when an item is deleted from the cache: Create a local variable that raises the event for the CacheItemRemovedCallback delegate as: private static CacheItemRemovedCallback onRemove = null; Create an event handler to respond when the item is removed from the cache as: static bool itemRemoved = false; static CacheItemRemovedReason reason; public void RemovedCallback(string key, object value, CacheItemRemovedReason callbackreason) { itemRemoved = true; reason = callbackreason; }
  • 31. Developing Web Applications Using ASP.NET How to: Implement Deletion Notifications in Cached Data (Contd.) Create an instance of the CacheItemRemovedCallback delegate that calls the event handler as: onRemove = new CacheItemRemovedCallback(this.RemovedCallback); Add the item to the cache by using the Cache.Insert method as: Cache.Insert("MyData1", Source, null, DateTime.Now.AddMinutes(2), NoSlidingExpiration, CacheItemPriority.High, onRemove);
  • 32. Developing Web Applications Using ASP.NET In this session, you learned that: ViewState is the mechanism for preserving the contents and state of the controls on a Web page during a round trip. ASP.NET enables controls to preserve their ControlState even when ViewState is disabled. The Application and Session objects enable you to cache information for use by any page in your ASP.NET application. ASP.NET maintains a single Application object for each application on your Web server. An ASP.NET Session object is an object that represents a user’s visit to your application. Summary
  • 33. Developing Web Applications Using ASP.NET The possible storage modes for storing session state information are: InProc StateServer SQLServer ASP.NET has a powerful caching system that you can use to improve the response time of your application. The ASP.NET caching system automatically removes items from the cache when system memory becomes scarce. Summary (Contd.)