Published: 25 Jan 2012
By: Bipin Joshi
Download Sample Code

HTML5 Web Storage provides a streamlined data storage mechanism that allows developers to store data on the client as key-value pairs. This article explores what Web Storage is, its flavors and illustrates how Web Storage data can be transferred between client and server in ASP.NET websites.

Contents [hide]

Introduction

Most of the websites developed today deal with data in one or the other form. It follows that application data needs some storage mechanism. As far as server is concerned we have sophisticated storage mechanisms such as an RDBMS. However, storing data on the client side can be tricky. Traditionally developers used Cookies to persist data on the client side but with many limitations. To help developers get rid of these limitations and provide a streamlined data storage mechanism HTML5 has introduced what is called as Web Storage. This article explores what Web Storage is, its flavors and using Web Storage in ASP.NET websites.

What is Web Storage?

Web Storage allows you to persist data on the client side as key-value pairs. The W3C recommended storage limit for Web Storage is 5 MB per origin. Comparing this storage size with that of Cookies (4 KB), it can be clearly seen that Web Storage offers a reasonable space for client side storage.

There are differences, however, between Cookies and Web Storage in terms of how they work. Cookies are passed between client and server with each and every request of a given website. On the other hand, Web Storage is never passed to the server automatically. If you ever need to transmit data from Web Storage to the server side code, you must resort to some programmatic approach such as jQuery calling a Web Method or a WCF service. Additionally, unlike cookies you cannot set expiration time for Web Storage. You either need to clear it programmatically or through some user interface dialog offered by the browser.

The term Web Storage is used to indicate client side data storage mechanism of HTML5. Web Storage comes in two flavors viz. Session Storage and Local Storage. These two types are exposed as sessionStorage and localStorage attributes of the window object.

As you might have guessed, Session Storage is persisted as long as the current browser (or its tab) instance is running. The moment you close the browser instance (or tab) the persisted data is removed. If you load the page again, it won't be able to see any of the previously stored data. Session Storage is suitable for a single transaction. Local Storage, unlike Session Storage, persists data across multiple instances of the browser and that too beyond current session. Thus closing a browser window won't remove the data. The data will be stored locally for future runs of the website.

Session Storage and Local Storage objects

As mentioned earlier sessionStorage and localStorage objects store data as key-value pairs. They have similar set of properties and methods. The following table lists them for your quick reference:

Table 1: Session Storage and Local Storage methods and properties

Property / Method

Description

setItem()

setItem() method stores a key-value pair in the respective Web Storage object.

getItem()

getItem() method retrieves value of a specified key from the respective Web Storage object.

removeItem()

removeItem() method removes a specified key-value pair from the Web Storage object under consideration.

clear()

clear() method removes all the key-value pairs from the Web Storage object.

key()

key() method takes a 0 based index and returns key name at that index.

length

length property returns the total number of key-value pairs present in the Web Storage object.

remainingSpace

remainingSpace property returns the amount of storage space in bytes still available for storing data. This property is specific to IE. Other browsers may implement similar property.

Now that you have some idea about session storage and local storage, let's try out the above properties and methods in a sample web form. Though you can use plain HTML page to test these properties and methods, we will use an ASP.NET web form so that you can modify the same form when we develop a more realistic example in the later sections.

Begin by creating a new ASP.NET website and design the default web form as shown in Figure 1. The complete markup of the web form can be found in the code download associated with this article. You will be using jQuery to handle button click events and to store data in Web Store. So you need to refer jQuery library in your website. The examples discussed in this article use IE9 as the browser but you can use any other browser that supports Web Store feature of HTML5 (including the latest versions of FireFox, Safari, Opera and Chrome).

Figure 1: Storing and retrieving data in Web Storage

Storing and retrieving data in Web Storage

As you can see, the form consists of two textboxes to enter key name and value respectively. Clicking the Store Data button saves the key-value pair to the localStorage and clicking on the Clear Data button clears all the key-value pairs. The table below the buttons reflect the latest key-value pair and total number of items in the localStorage.

The following jQuery code shows how the above form works:

First, the code stores a reference to localStorage object in a variable - storage. This way you can easily test the code for sessionStorage also by changing just one line. The ready event handler of the document wires click event of the two buttons - Store Data and Clear Data - with event handler functions namely OnStoreClick() and OnClearClick().

The OnStoreClick() event handler function stores a key-value pair in the storage object using setItem() method. It then uses key() and getItem() methods are then used to retrieved the key just stored. The key-value pair along with the length of the storage is then displayed in respective DIV elements.

The OnClearClick() method removes all the items from the store using clear() method. This can be verified by outputting length of the storage again.

Session Storage and Local Storage Events

The sessionStorage and localStorage objects support storage event that is raised whenever the underlying storage area changes. You should be aware of two things while dealing with this event. Firstly, storage event is raised on the window object. Secondly, for most of the browsers except IE9, storage event is fired on every browser instance (or tab) except for the one that changed the storage object. In case of IE9 storage event is raised for all the instances of the browser. So, if Example1.aspx is loaded in three browser windows Window1, Window2 and Window3 and Window1 changes the web storage, Window2 and Window3 will receive the storage event. In IE9 all the windows receive the storage event.

The storage event handler receives event information as a StorageEvent object whose properties are shown in the following table:

Table 2: StorageEvent

Event data

Description

key

Represents the key of the item that is being changed.

oldValue

The old value of the key (if any) being changed

newValue

The new value being assigned to the key

url

The url of the page that is changing the storage area

storageArea

Reference to the storage area

Just to check how the storage event works, modify the previous code as shown below:

Notice how storage event handler has been attached using addEventListener() method of the window object. The OnStorage() function uses various pieces of event parameter and displays them in a message box. The Figure 2 shows a sample run of the form when a new key is added:

Figure 2: Handling storage event

Handling storage event

Clearing Web Storage Manually

If you add some key-value pairs to the localStorage and then close the browser without clearing the storage area, the data is preserved on the disk. As mentioned earlier, unlike Cookies you cannot set any specific expiration date and time for local storage. One way to clear the localStorage data is programmatically calling the removeItem() method for each item or by calling the clear() method. Alternatively, you can manually delete the data using the browser dialogs. For example, have a look at Figure 3 that shows "Delete Browsing History" dialog of IE9.

Figure 3: Clearing localStorage manually

Clearing localStorage manually

There are two checkboxes you should be aware of - Cookies and Preserve Favorites website data. Un-checking the Cookies checkbox will clear all the storage areas for all domains that are not in your favorites folder. If you want to clear everything make sure to un-check Preserve Favorites website data checkbox.

Passing data from Web Storage to server

Unlike Cookies, Web Storage data is not passed between the client and server with each and every request. If you wish to send the Web Storage data to the server you must device a programmatic way to do so such as a hidden field or an AJAX call. As far as ASP.NET web forms are concerned one neat way is to call a Web Service or a WCF Service from the client script and pass the data to the server. The server can then examine and process the data and may store it into a database.

In the example discussed in the next section, you will use jQuery to call Web Methods. The Web Methods perform two functions - they fetch data from the server so that client can render it as required and they persist the Web Storage data in SQL Server database. While dissecting the example, our focus will be on Web Storage related functionality and we won't go into the details of making AJAX calls using jQuery.

A more realistic example - Survey

Now that you know everything needed to make use of Web Store features of HTML5, let's develop a more realistic example than the previous one that will put to use all the information we discussed so far. In this section you will develop a simple survey form that captures user feedback. As you might be aware, filling up a survey may not be a priority for the end user while he is using your website. Chances are that he may start filling it and navigate to some other part of the website that interests him more. He may even close the browser and come back to your website sometime later. In any such scenarios it would be nice to persist the survey data locally as the user is entering it. Later when user comes back, you can reload the persisted data and save him some time. Your survey form will look as shown in Figure 4:

Figure 4: Survey form developed using Local Storage

Survey form developed using Local Storage

To begin developing the survey form, create a new ASP.NET website and add a SQL Server database to its App_Data folder. You will need to design four tables viz. Questions, Choices, Users and Results. The schema of these tables is shown in Figure 5:

Figure 5: Tables required in Survey form

Tables required in Survey form

The Questions table stores survey questions and QuestionID is the primary key. The Choices table stores choices for questions and its primary key is ChoiceID. One question can have multiple choices. The Users table stores user information such as FirstName, LastName and Email and its primary key is UserID column. Finally, Results table stores user response to the survey questions.

To get the data in and out of these tables, you will use Entity Framework. So, add a new ADO.NET Entity Data Model to the website. Drag and drop all the four tables onto the surface of the designer so as to create data model classes as shown in Figure 6:

Figure 6: Entity Data Model for required tables

Entity Data Model for required tables

The communication between client and server will happen via jQuery and Web Methods. You will need to write three web methods in all viz. GetQuestions(), GetChoices() and SaveResults(). These methods are discussed next.

The GetQuestions() Web Method is intended to return all the survey questions from the Questions table. As you can see, it simply returns all the Question instances to the caller.

The GetChoices() Web Method is similar to GetQuestions() method but returns all the choices from Choices table.

The SaveResults() Web Method saves user response to the survey in the database. In order to understand the SaveResults() method you need to understand how you will be storing data in the localStorage. The following table lists the keys used to store data in the localStorage.

Table 3: Local Storage keys to store data

Key

Value

FirstName

A string value indicating first name of the user.

LastName

A string value indicating last name of the user.

Email

A string indicating Email address of the user.

<choice_id>

<question_id> - An integer indicating QuestionID of the choice. E.g localStorage["3"] = 1 where 3 is a ChoiceID and 1 is a QuestionID

Notice that localStorage is a key-value collection and each key needs to be unique. That is why we make ChoiceID as key and QuestionID as value. If you do other way round, you will not be able to store multiple choices for a question because latest ChoiceID will overwrite the previously stored ChoiceID since QuestionID for both will be the same.

The SaveResults() method accepts a Dictionary that will contain all the key-value pairs from the localStorage. Inside it inserts a record in the Users table and fetches back its UserID. It then removes FirstName, LastName and Email keys from the dictionary so that you can iterate through the remaining keys and with each iteration inserts a record in the Results table.

The web methods just discussed are called from the client side jQuery code. Let's see how.

The ready event handler wires click event of the "Submit Answers" button to SubmitData function (discussed later). It also wires change event of three textboxes - firstName, lastName and email - to a function. The change event handler function essentially stores value of the respective textbox in the localStorage. This way even if user navigates away by filling the survey form half way, you still have the data with you. Just in case data is already there in the localStorage you load it in the textboxes. Survey questions are dynamically fetched from the database and displayed to the user. This is done inside the GetQuestions() function.

The GetQuestions() function calls GetQuestions() Web Method you wrote previously. To call the GetQuestions() Web Method you use jQuery .ajax() function. Notice how the URL is specified in the .ajax() call. Make sure to change it as per the web form URL at your end.

The successHandler function is called upon successful completion of the Web Method and receives all the Question instances as an array. You then iterate through the questions and with each iteration append a <DIV> element to the container. Notice how the code uses data-* attributes of HTML5 to store QuestionID. This way you can easily find out the QuestionID for a particular question in later code. Markup of a sample <DIV> element added by the above code is shown below:

Once all the questions are loaded, you need to load all the choices. This is done by calling another function - GetChoices(). The GetChoices() function is shown below:

The GetChoices() function is similar to GetQuestions() function in that it calls a Web Method using jQuery .ajax() call. This time, however, it calls GetChoices() Web Method. You need to append all the choices belonging to a question inside that question's <DIV> element. This is done by finding a <DIV> element whose data-questions-questionid attribute matches with the QuestionID of the current choice. An <INPUT> checkbox element and a <SPAN> element is then added to that <DIV>. Notice how the code sets data-choices-questionid and data-choices-choiceid attributes of the <INPUT> element for later use. These two attributes represent QuestionID and ChoiceID of a choice respectively.

Whenever user toggles a checkbox, you need to either store the user choice in the localStorage (checked) or remove it from the localStorage (unchecked). This is done by handling change event of the checkboxes. The change event handler essentially checks whether a checkbox is being checked or unchecked. Accordingly it adds or removes an entry to the localStorage.

When a user fills out the survey form and clicks on the "Submit Answers" button, SubmitData() function is called. This function in turn calls SaveResults() Web Method. The following code shows how this is done.

The SubmitData() function forms a JSON representation of all the key-value pairs from the localStorage by iterating through all the keys. The following JSON string represents a sample set of key-value pairs from the localStorage.

Notice how each key and value is enclosed in double quotes. Also note that "data" is the parameter name of the SaveResults() Web Method. This way ASP.NET maps the incoming JSON data with a particular parameter of a Web Method.

Once the SaveResults() Web Method returns successfully, all the data from the localStorage is removed using clear() method.

You can now run the web form and test the functionality by entering some data. Also verify that the data gets stored in the localStorage even if the browser is closed and is loaded back when the survey form is visited again.

Summary

Web Storage features offered by HTML5 allow you to store data on the client machine. The two objects - sessionStorage and localStorage - store key-value pairs of data. The sessionStorage can persist data only for the current session whereas localStorage can persist data across sessions. Web Storage is not transmitted to the server automatically. You need to device some programmatic approach such as AJAX calls to a web service or WCF service, to send the Web Storage data to the server.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Bipin Joshi

Bipin Joshi is a blogger, author and a Kundalini Yogi who writes about apparently unrelated topics - Yoga & Technology! A former Software Consultant and trainer by profession, Bipin is programming since 1995 and is working with .NET framework ever since its inception. He is an internation...

This author has published 7 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository and Unit of Work Patterns, and MVC3 Razor View
A detailed introduction about the code first approach using Entity Framework 4.1, Inversion of Contr...
jQuery Mobile ListView
In this article, we're going to look at what JQuery Mobile uses to represent lists, and how capable ...
Exception Handling and .Net (A practical approach)
Error Handling has always been crucial for an application in a number of ways. It may affect the exe...
JQuery Mobile Widgets Overview
An overview of widgets in jQuery Mobile.
Book Review: SignalR: Real-time Application Development
A book review of SignalR by Simone.

You might also be interested in the following related blog posts


Whats New for the HTML/JavaScript Developer in RadControls for Windows 8 HTML and Telerik Reporting - Q3 2013 Webinar resources read more
HTML5 Threading with Web Workers and Data Storage with IndexedDB - MSDN Article read more
Two Ways of Passing HTML5 Web Storage Data to ASP.NET read more
HTML5 Web Storage (Example : TODO) read more
Building Hugely Performant Websites: Taking advantage of HTML5 Storage, 3rd DevConnections Orlando Presentation read more
HTML5 Web Storage in Essence read more
Developing for the web using VS 2010 and .NET 4 read more
Web-based Scheduling Solution with the WOW-Factor for ASP.NET 2.0 and Up read more
Q3 2009 Release Week kicks-off November 4th, Free Daily Webinars read more
ASP.NET 4 Web Server Here Shell Extension read more
Top
 
 
 

Discussion


Subject Author Date
placeholder Store Data From Server side code. Shafaqat Shafaqat 2/7/2012 8:34 AM
RE: Store Data From Server side code. Andrew Alger 2/7/2012 11:16 AM
placeholder Great Article - ASP.NET MVC version Rick Anderson 3/12/2012 9:08 PM

Please login to rate or to leave a comment.