Click here to Skip to main content
15,860,943 members
Articles / Web Development / ASP.NET
Article

Navigation Tips in Asp.net

Rate me:
Please Sign up or sign in to vote.
2.70/5 (8 votes)
21 Sep 2007CPOL1 min read 37.1K   23   3
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:

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

//
// 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:

<p>Use <code>Page.SetFocus </p>

<p>(eg:) If the Login is invalid then its easy to focus the cursor to user name.</p>
</code /> 
<pre>//

//
// 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//
</pre>

<h3>Navigation Tip 3:</h3>
<code>
<p>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.</p>

<p>And also we can use <code>location.replace </p>

<pre>//

//
// 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>//
</pre>
<code>
<p>A brief desciption of how to use the article or code. The class names, the methods and properties, any tricks or tips. </p>

<p>Remember to set the Language of your code snippet using the Language dropdown.</p>

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMaintainScrollPositionOnPostBack Pin
Brent Lamborn5-Aug-07 7:57
Brent Lamborn5-Aug-07 7:57 
GeneralRe: MaintainScrollPositionOnPostBack Pin
Thirumal S5-Aug-07 18:30
Thirumal S5-Aug-07 18:30 
GeneralHi thirumal Pin
gaya_moni3-Aug-07 22:02
gaya_moni3-Aug-07 22:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.