Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Maintaining Scroll Position - Article Translation
Message
De
13/07/2005 08:21:26
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
13/07/2005 02:48:12
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 1.1
OS:
Windows 2000 SP4
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01032073
Message ID:
01032145
Vues:
17
This message has been marked as the solution to the initial question of the thread.
>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);
	}
}
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform