Controlling Browser History
It has been said that "he who controls history, controls the future." That is certainly true when using a browser. Everyone is familiar with the browser Forward and Back buttons, which allow the user to navigate back and forth amongst the pages already visited in the current session. Forward and Back are central to the browsing experience.
The browser automatically enables and disables these buttons as necessary, if there are appropriate pages to navigate to. For example, if you are at the first page visited that session, then the Back button will be disabled because you can't go back any further.
The browser maintains a history which contains a list of all the URLs sent back to the server as page requests. However, it is here that the benefits of AJAX turn around
and bite us. With asynchronous postbacks, the URL is not posted back to the server as a page request. This means that AJAX asynchronous postbacks are not added to the browser's history, do not affect the Forward or Back buttons, and are not accessible to those buttons or in any history lists. Bummer.
You can see this in the previous example, AdventureWorksRevisited. Run that example again, and notice that the Forward and Back buttons in your browser are disabled—that's because this is the only page you've visited so far. Now change the radio buttons that control the display of personal information. Even though the content of the page appeared to change, neither the Forward nor Back buttons are enabled. This is because a full page request is not being made of the server, so the browser doesn't know that there's been a request and response from the server.
Out in the real world of deployed web sites, if the navigation buttons are enabled as a result of previously navigating to other pages, they will not take you where you intuitively think you ought to go if you've used any AJAX-enabled controls. And worst of all in that case, you will lose any work you have done in any AJAX-enabled controls on the page.
With ASP.NET 3.5 Service Pack 1, you can fix this, although it does take some coding on your part (something we try to avoid as much as possible in this book). There are two things you need to deal with. The first is to create a history point and add it to the browser history when the state of the page changes. The second is to handle the clicks when the user clicks the brower's Forward and Back buttons or history lists and to restore the state of the page as it was earlier. The ScriptManager control included on every ASP.NET AJAX web page, along with the UpdatePanel control, provides the means to accomplish both of these ends.
An example will show how this works. Create a new ASP.NET web site called AjaxHistory.
Before you can manage browser history, you must add and configure the ScriptManager control. In either Source or Design view, drag a ScriptManager control from the AJAX Extensions section of the Toolbox onto the page. Then, put the cursor on the ScriptManager control so that its properties are visible in the Properties window. Set the EnableHistory property to True, as shown in Figure 3-4. With this property set, history is enabled for all AJAX-enabled controls on the page, that is, for all controls inside UpdatePanel controls. The default setting of this property is False, which is why the Forward and Back buttons were disabled when you tried them a moment ago.
Drag an UpdatePanel control from the AJAX Extensions section of the Toolbox onto the page. You will place all the other controls whose history is to be tracked inside this UpdatePanel. For this example, the page will contain a RadioButtonList with
AjaxH'rstory - Visual Web Developer 2008 Express Edition File Edit View Website Build Debug Tools Window Help
Toolbox ▼ JJ !al HiddenField
Literal ^ Calendar g AdRotator %) FileUpload V Wizard g Xml GQ| MultiView H Panel !S| PlaceHolder Q View 1^1 Substitution ■¿£1 Localize EJ Data i±i Validation
El Navigation Ei Login
El WebParts
G AJAX ExtensL.
Pointer
113 ScriptManager] ^ ScriptManag... 0 Timer ^ UpdatePane!
Post a comment