Managing Browser History by Using Client Script

Introduction

As a page developer, you can manage browser history entries and provide logical navigation by using the System.Web.UI.ScriptManager and System.Web.UI.ScriptManagerProxy server controls. You can also manage the browser history through client script. History support in the client can be enabled through the ScriptManager control. This generates client objects that you can use to manage browser history.

A history point is a logical navigation point in the Web application that can be represented through state information. The state information can be used to restore the Web application to a previous state, either directly with state data or through some identifier to state information that is stored elsewhere.

History points are stored in the browser's history stack only as URLs. History state is managed as data in a query string or as a URL fragment value that is marked with the "#" character. Because of size restrictions on URLs, the state information you create should be as small as possible. The following example shows a URL that contains enough history point data to identify the state. From this, the application can recreate the page at that state.

http://MySamples/Ajax/Default.aspx#state=2

When a user clicks the browser's Back button, the browser navigates through previously-viewed URLs, which will include URLs that contain history-point state. Client code in the Web page detects that the URL contains history state data (either server-based or client-based) and raises the client Sys.Application.navigate event. You can handle the event to reset the application to the state whose information is contained in the parameter values that are passed to the event.

To run the example code in this topic, you will need the following:

  • ASP.NET Extensions. You can download the preview release from the ASP.NET Web site.

Enabling Browser History Management

In order to use history management, you must enable it through the ScriptManager server control. By default, history support is not enabled. When history is enabled, it is implemented differently for each browser. For Internet Explorer (IE), an iframe element is rendered to the client, which can result in an additional request to the server. The model is therefore an opt-in approach. The following example shows how to enable history declaratively through the ScriptManager control.

<asp:ScriptManager runat="server" ID="ScriptManager1" EnableHistory="true" />

Creating Browser History Points

To create a browser history point, you call the Sys.Application.addHistoryPoint method. This method allows you to define the history entry, its title, and any state required. You can use the state data to recreate the state of the page when a subsequent history navigation event is raised.

When you add a history point, or when the page is navigated and contains history state in the URL, the Sys.Application.navigate event is raised. This provides an event in the client that notifies you that history state has changed. You can handle the navigate event to recreate objects with state data or perform operations.

The following example shows how you can manage history points in client code.

Language:
Run View

See Also

This topic is ASP.NET Extensions documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.