Friday, August 24, 2007

SmartNavigation Vs MaintainScrollPositionOnPostback in ASP.net 2.0

MaintainScrollPositionOnPostback in ASP.net 2.0

Every one of us know very well about the feature of "SmartNavigation", BUT In Visual studio version 2.0 and above "SmartNavigation" has become obsolete. Now What is the alternate for this, if you want to achieve same feature as "SmartNavigation" then use "MaintainScrollPositionOnPostback".

How to use

Just set Page.MaintainScrollPositionOnPostback = true in page load event and java script will be inserted into your rendered page that maintains the scroll position in the browser window for all post back.
Believe me it's really a good feature in ASP.net 2.0, it would reduce lots of coding effort to maintain scroll position in web page.

Another alternate to maintain page scroll position

This way is same what we do for HTML documents. The following lines you can write into the server side or client side. Below example to write in server side

StringBuilder maintainScrollPosition = new StringBuilder();

maintainScrollPosition.Append("<script language='javascript'>");
maintainScrollPosition.Append("var pageSize = 722;");
maintainScrollPosition.Append("if (document.frmHome.offsetHeight > pageSize){");
maintainScrollPosition.Append("location.href='#scrollTo" + entity[0] + "';}");
maintainScrollPosition.Append("</script>");

Page.ClientScript.RegisterStartupScript(this.GetType(), "Contents", maintainScrollPosition.ToString());

Here entity[0] has a value where you want to set scroll position, in other word it's a flag.You can assume entity[0] is heading in your aspx page and after postback you want scroll position be on this heading.

Note: Above example also explains how we can write java script in code behind (C#)

2 comments:

gaurav kesarwani said...

Hello Ritesh,

awsome blog i must say, came to c ur blog via kesarwani website....
u can get in touch wid me at

http://urmostwelcometomyworld.blogspot.com/

or gaurav.kesarwani@hcl.in
im business analyst...wat abt u...in which company r u working ??
do keep in touch...

daspeac said...

I have heard about another way of file recovery for clipper. Besides, you can visit my blogs at: http://daspeac.livejournal.com/ or http://daspeac.blogspot.com/ where I’m trying to share my experience with regard to data corruption issues.