In earlier versions of ASP.NET, you enable smart navigation by using the
Page.SmartNavigation property. When you set the
Page.SmartNavigation property to
true, the following smart navigation features are enabled:
- The scroll position of a Web page is maintained after postback.
- The element focus on a Web page is maintained during navigation.
- Only the most recent Web page state is retained in the Web browser history folder.
- The flicker effect that may occur on a Web page during navigation is minimized.
This article describes how to implement the smart navigation features in ASP.NET 2.0 without using the
Page.SmartNavigation property.
How to maintain the scroll position
To maintain the scroll position of a Web page after postback, use the
Page.MaintainScrollPositionOnPostBack property. For more information about the
Page.MaintainScrollPositionOnPostBack property, visit the following Microsoft Developer Network (MSDN) Web site:
How to maintain the element focus
To maintain the element focus on a Web page during navigation, use the
Page.SetFocus method. For more information about the
Page.SetFocus method, visit the following MSDN Web site:
How to retain only the most recent Web page state in the Web browser history folder
To prevent a user from going back to previously visited Web pages, you must prevent visited Web pages from being added to the Web browser history folder. Additionally, you must prevent postbacks that are generated by ASP.NET server controls from being added to the Web browser history folder. If only the most recent Web page state is retained, and if no Web pages are in the Web browser history folder,
Back is unavailable.
By design, you cannot modify the Web browser history folder programmatically. To work around this restriction, use one of the following methods.
Method 1: Disable the Web browser cache, and use session variables
If you disable the Web browser cache, Microsoft Internet Explorer retains only pointers to the visited Web pages in the Web browser history folder. Internet Explorer does not retain the actual content for the Web pages. Therefore, when a user clicks
Back, the Web browser must submit a page request to the Web server. By using ASP.NET session variables, you can write an algorithm that determines whether the user should be able to view the requested page. If problems would occur in the Web application when a user viewed the requested page, the Web server can redirect the Web browser to the current page instead. Therefore, nothing appears to happen when the user clicks
Back.
To disable the Web browser cache, use one of the following methods:
- Disable the Web browser cache by using the ASP.NET @ OutputCache directive. When you set the Location attribute to None, the Web browser cache is disabled. For more information about the @ OutputCache directive, visit the following MSDN Web site:
- Disable the Web browser cache programmatically by using the Response.Cache.SetCacheability method. For more information about the Response.Cache.SetCacheability method, visit the following MSDN Web site:
- Disable the Web browser cache by using the <meta http-equiv> HTML tag in a Web page. The following HTML code example demonstrates how to do this.
<html>
<head>
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
</head>
Method 2: Use a hidden frame in a Web page
This method resembles the mechanism that is used internally by smart navigation. Create a Web page that contains a visible frame and a hidden frame. The following HTML code example demonstrates how to do this.
<frameset cols="100%,*">
<frame src="goback.html">
<frame src="fillerpage.html">
</frameset>
Both frames must reference an existing Web page. Otherwise, the Web browser window displays an error message that the Web page cannot be found. The hidden frame page (Fillerpage.html) must contain no content. The visible frame page (Goback.html) must contain the following HTML code.
<meta http-equiv="refresh" content=".0; url=nobackpage.html">
When a user visits the Web page, both frames are loaded. The Goback.html page immediately redirects the visible frame to a new Web page (Nobackpage.html). If the user clicks
Back, the Goback.html page is loaded. Then, the Goback.html page immediately redirects the user to the Nobackpage.html page again.
Method 3: Use the location.replace method
Create a Web page that runs a client-side script that calls the
location.replace method. In this case, the Web browser loads the content of a URL in the active window. Because the content is replaced in the active window, the Web browser does not consider this replacement to be navigation between Web pages. Therefore, no entries are added to the Web browser history folder. The following HTML code example demonstrates how to use the
location.replace method.
<a href="next.aspx" onclick="javascript:location.replace(this.href); event.returnValue=false; event.cancelBubble=true;">
Click here to visit the next page without adding the current page to the history folder.
</a>
The following code example demonstrates how to use the
location.replace method in a C# method.
private void WebForm1_PreRender(object sender, System.EventArgs e)
{
if (IsPostBack)
{
Response.Write("<html><head><script>location.replace('"+Request.Path+"');\n"+"</script></head><body></body></html>\n");
Response.End();
}
}
For more information about the
location.replace method, visit the following MSDN Web site:
Method 4: Use the window.history.forward method
Create a Web page that runs a client-side script that calls the
window.history.forward method. In this case, the Web browser automatically advances one page in the Web browser history folder. Therefore, later postbacks are added to the Web browser history folder. If a user clicks
Back, the user is redirected to the current Web page.
You can use the
window.history.forward method together with the
location.replace method. When you use these methods together, navigation between Web pages and postbacks are handled correctly.
For more information about the
window.history.forward method, visit the following MSDN Web site:
Method 5: Modify the Web application logic
Modify the Web application logic to work correctly when a user clicks
Back. The Web application must maintain the integrity of all the submitted data and the integrity of the user state when the user clicks
Back.
How to minimize the flicker effect that may occur during navigation
When a user views a Web page in an ASP.NET Web application that uses server controls, the user may experience a flicker effect. The flicker effect may occur when the user changes the value of a control. If the control generates a postback, the Web browser submits a request for a new Web page state to the Web server. When the new Web page state is rendered in the Web browser, the flicker effect may occur.
There are no simple ways to eliminate this flicker effect, because the Web page must be rendered again. Typically, this behavior is noticeable to the user.
Note When the connection speed between the Web client and the Web server is very fast, the flicker effect may be unnoticeable.
To minimize the flicker effect, minimize the number of postbacks, or eliminate postbacks. To do this, use one of the following methods, as appropriate for your situation.
Method 1: Use the ASP.NET 2.0 client callback manager
Use the ASP.NET 2.0 client callback manager to let Web pages submit requests to the Web server without using a full postback. Because the client callbacks do not include postback data, the client callbacks do not force the whole Web page to be updated in the Web browser. This minimizes the flicker effect that may occur during navigation.
For more information about the ASP.NET 2.0 client callback manager, visit the following MSDN Web site:
For more information about how to implement client callbacks without using a postback, visit the following MSDN Web site:
Method 2: Use a hidden frame in a Web page
Create a Web page that contains a visible frame and a hidden frame. For more information about how to do this, see method 2 in the "How to retain only the most recent Web page state in the Web browser history folder" section.