Retain page postition on button click
Hi,
I'm using a datagrid which may or may not be important, and am adding rows to it, when I add a new row to the grid the page is reloaded and the page goes back to the top I then have to scroll back down to where I was working. Does anyone know how to make it load back to it's original position?
Hope this makes sense if not please let me know
Cheers Al
Re: Retain page postition on button click
You can do this in javascript. You are gonna need to set the X and Y values of the page's position when u scroll down the page. This is all done in the body tag:
Code:
// setting coords of page
function ScrollItPage()
{
window.scrollTo(document.Form1.PageX.value, document.Form1.PageY.value);
}
function setcoordsPage()
{
var myPageX;
var myPageY;
if (document.all)
{
myPageX = document.body.scrollLeft;
myPageY = document.body.scrollTop;
}
else
{
myPageX = window.pageXOffset;
myPageY = window.pageYOffset;
}
document.Form1.PageX.value = myPageX;
document.Form1.PageY.value = myPageY;
}
In your body tag:
Code:
<body onscroll="javascript:setcoordsPage()" onload="javascript:ScrollItPage()"
MS_POSITIONING="GridLayout">
Hopefully this helps.
Re: Retain page postition on button click
I've found that there is a setting IE called SmartNavigation.
VB Code:
[HL="#FFFF00"]<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="EditGrid.WebForm1" [b]smartNavigation="True"[/b]%>[/HL]
Does anyone have any idea's for making this work with other browsers?
Re: Retain page postition on button click
Patch21,
Thanks for that, I must have just missed your post, I'll check your code out thanks
Cheers Al
Re: Retain page postition on button click
Patch,
It's doesn't seem to be working? My page gets submitted to the server on each reload. Would that make any difference?
Cheers Al
Re: Retain page postition on button click
Smartnavigation is a buggy P.O.S. feature in ASP.NET development. It's better to use javascript for this which will work across browsers, instead of relying upon SmartNavigation.
Re: Retain page postition on button click
Mendhak,
Thanks for the heads up.
Does anyone have a working script? In the interests of not reinventing the wheel and all that!! Oh and saving time to. :D
Cheers Al
Re: Retain page postition on button click
What errors are u getting with the javascript above ?
Re: Retain page postition on button click
Did you create two hidden fields called PageX and PageY?
Re: Retain page postition on button click
Ah yeah sorry, forgot to mention that :blush:
Bit of a hack, but works well for me. You need 2 hidden fields with ID's PageX and PageY.
Re: Retain page postition on button click
That's how SmartNavigation works too. It's not an IE feature, but it's a logic and code implementation just like this.
Re: Retain page postition on button click
Thanks guys,
Apologies for the delay in responding I've been away for a couple of days.
I found this on Friday which seems to work, what do you think?
VB Code:
Private Sub registerAnchorScript(ByVal strAnchorID As String)
' Source: [url]http://geekswithblogs.net/jawad/archive/2005/05/25/BookMarkJump.aspx[/url]
Dim sScript As New StringBuilder
sScript.Append("<script language=JavaScript id=""BookMarkScript"">" & vbCrLf)
sScript.Append("var hashValue=""#" & strAnchorID & """;" & vbCrLf)
sScript.Append("if(location.hash!=hashValue)" & vbCrLf)
sScript.Append("location.hash=hashValue;" & vbCrLf)
sScript.Append("</script>" & vbCrLf)
If (Not Page.IsStartupScriptRegistered("BookMarkScript")) Then
Page.RegisterStartupScript("BookMarkScript", sScript.ToString)
End If
End Sub
Private Sub cmdAddImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddImage.Click
' Call add another row to the submit files datagrid
' Return the original page position
registerAnchorScript("graphicsfiles")
End Sub
Requires an anchor on the page where you want to jump to:
VB Code:
<a href="#" id="jumphere"></a>