Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Navigation Tips in Asp.net

0.00/5 (No votes)
21 Sep 2007 1  
This article explains how to Disable the Back button and fix the scroll bar while postback

Introduction

This article describes how to Disable the Back button in IE Browser and the scroll position of a Web page is maintained after postback.

Usally all the controls in Asp.net use runat server so it will postback for every action . while refreshing , the scroll bar again goes to top position of the browser. To avoid this problem use

Page.MaintainScrollPositionOnPostBack property is TRUE.

Navigation Tip 1:

As already explained use the Page.MaintainScrollPositionOnPostBack property in Page load event it will maintain the scroll positioin while postback.

//


//

// To Maintain the Scroll bar Position

//


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try

Page.MaintainScrollPositionOnPostBack = True

Catch ex As Exception
Throw ex
End Try

End Sub//

Navigation Tip 2:

Use <code>Page.SetFocus

(eg:) If the Login is invalid then its easy to focus the cursor to user name.

</code />
//


//

// To Maintain the focus for the Control//


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try
'To Maintain the focus for the Control
Page.SetFocus(Control)

Catch ex As Exception
Throw ex
End Try

End Sub//

Navigation Tip 3:

Usually the web browser cache all the pages in the history so the client can able to navigate to previous page using the Back button to avoid this problem use "no-cache" in your page to resitrict the end user to navigate to Previous page.

And also we can use <code>location.replace

//

//
// To Disable the Back Button using Response.CacheControl//

<%
Response.Buffer = True
Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.CacheControl = "no-cache"
%>//

//
// To Disable the Back Button using Java Script//
<script language="JavaScript">
<!--// Method 1
javascript:window.history.forward(1);

// Method 2 
javascript:location.replace(this.href); 
//-->
</script>//

A brief desciption of how to use the article or code. The class names, the methods and properties, any tricks or tips.

Remember to set the Language of your code snippet using the Language dropdown.

Use the "var" button to to wrap Variable or class names in <code> tags like this.

:
method or window.history.forward(1) to avoid the same.Property to Maintain the focus for a given control while postback. you can use this property in Login Form to set focus.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here