Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Maintaining Scroll Position - Article Translation
Message
From
14/07/2005 15:34:43
 
 
To
13/07/2005 08:21:26
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 1.1
OS:
Windows 2000 SP4
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01032073
Message ID:
01032785
Views:
13
>>VS.NET 2003
>>
>>I have some relatively long forms in my ASP.NET app. When there is a postback, the form jumps back to the top of the page which users are finding annoying.
>>
>>This can apparently be worked around in a couple of ways - one elegant way is outlined at http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309bm_l/asp200309bm_l.asp
>>
>>I'm still a bit of a newbie in all this - it looks like the article assumes I know just a little more than I actually do :) I can translate the VB.Net, I see where to put the Page.Load PostBack check but I don't see where to put the RegisterHiddenField() function call or the SaveScrollLocation() code i.e. how to hook it all together. Any pointers appreciated.
>
>Al,
>I think using SmartNavigation=true in @page directive would be better (as Viv pointed out already).
>However here is a translation:
>
>
>private void Page_Load(object sender, System.EventArgs e)
>{
>	if (!Page.IsPostBack)
>	{
>          // ...
>	}
>	RegisterHiddenField("__SCROLLLOC", "0");
>	StringBuilder sb = new StringBuilder();
>	sb.Append("<script language='javascript'>");
	sb.Append("function SaveScrollLocation () {");
	sb.Append("     document.forms[0].__SCROLLLOC.value = document.body.scrollTop;");
	sb.Append("}");
	sb.Append("document.body.onscroll=SaveScrollLocation;");
	sb.Append("</script>");
>	if (!IsStartupScriptRegistered("saveScroll"))
>	{ RegisterStartupScript("saveScroll", sb.ToString());	}
>	if (Page.IsPostBack)
>	{
>		StringBuilder _sb = new StringBuilder();
>		_sb.Append("<script language='javascript'>");
		_sb.Append("function SetScrollLocation () {");
		_sb.AppendFormat("     document.body.scrollTop = {0};",Request["__SCROLLLOC"]);
		_sb.Append("}");
		_sb.Append("document.body.onload=SetScrollLocation;");
		_sb.Append("</script>");
>		if (!IsStartupScriptRegistered("setScroll"))
>		{ RegisterStartupScript("setScroll", _sb.ToString()); }
>	}
>}
>
>
>Or in another way (w/o using StringBuilder):
>
>	RegisterHiddenField("__SCROLLLOC", "0");
>	string saveScroll =
>@"<script language='javascript'>
function SaveScrollLocation () {
     document.forms[0].__SCROLLLOC.value = document.body.scrollTop;
}
document.body.onscroll=SaveScrollLocation;
</script>";
>	if (!IsStartupScriptRegistered("saveScroll"))
>	{
>		RegisterStartupScript("saveScroll", saveScroll);
>	}
>	if (Page.IsPostBack)
>	{
>		string setScroll =
>@"<script language='javascript'>
function SetScrollLocation () {
     document.body.scrollTop = " + Request["__SCROLLLOC"].ToString() +
@";}
document.body.onload=SetScrollLocation;
</script>";
>	if (!IsStartupScriptRegistered("setScroll"))
>	{
>	RegisterStartupScript("setScroll", setScroll);
>	}
>}
>
Thanks, Cetin. I tried setting SmartNagivation on 2 test forms that could use scroll control. For maintaining scroll position, it worked for 1 of the 2 forms in IE, 0 of 2 in FireFox. When I tested the code shown above, 2 of 2 worked for both browsers.

I understand that SmartNavigation is supposed to do more - e.g. eliminate the "flash" when the page refreshes - but for just the task of maintaining scroll position it looks like the code you kindly translated above is the way to go. Thanks again.
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Previous
Reply
Map
View

Click here to load this message in the networking platform