Managing Browser History Using Client Script
As a page developer, you can manage browser history entries and provide logical navigation by using the ScriptManager and ScriptManagerProxy server controls. You can also manage browser history through client script. You can enable history support in the client 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 an 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 that you create must 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 re-create the page at that state.
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 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 work with the example code in this topic, you will need Visual Studio 2008 Service Pack 1 or a later release.
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, an iframe element is rendered to the client, which can cause 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 lets you define the history entry, its title, and any state that is required. You can use the state data to re-create 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 re-create objects by using state data or to perform other operations.
The following example shows how you can manage history points in client code.