I need to make the page maintain the scroll position on postback. I used the SmartNavigation and that caused lost focus on the field. I found the following javascript from
http://aspnet.4guysfromrolla.com/articles/111704-1.aspx. It seems working on the demo in the link, but not working in my page. I am using ASP.NET 2003. Can you please help? I am not sure
if the function or the window.onclick and the window.onkeypress does not work. I added the two hidden fields scrollLeft and scrollTop in the form. Thanks.
function SmartScroller_GetCoords()
{
var scrollX, scrollY;
if (document.all)
{
if (!document.documentElement.scrollLeft)
scrollX = document.body.scrollLeft;
else
scrollX = document.documentElement.scrollLeft;
Steve: you will see in his post he is using ASP 1.x and this property is not available.
Josh: I did notice his post said ASP.NET 2003. I wasn't aware this implied .NET 1.x
If there was ever a compelling reason to upgrade, this certainly qualifies!
2003 = 1.x
And a simple automatic script implementation is a sad reason to upgrade! (MaintainScrollPositionOnPostback will pretty much result in the exact script I posted, with two hidden fields, and a javascript call on the body onload). And have you gone through
the 2003 -> 2005 conversion process on a large-scale web app? It isn't all that fun, trust me.
The Javascripts ran but the scrollX and scrollY returned zeros always. I tried when I clicked a radio button at the bottom of the form. I checked by adding alert(scrollX); and alert(scrollY) after document.forms[0].scrollTop.value
= scrollY; Any idea? Thanks.
Does your team use the VSIP toolkit or maybe have the bulk of your app factored down to patterns and generators? Do you need in-house controls that will make your current vendors blush? If so, we need to talk.
(1) I changed the interval to 5000, so I would have enough time to scroll down to the bottom of the form.
(2) Yes, it is in a frame.
I also found out that it works if the control does not have auto postbask. If the control has auto postback, the scrollLeft and scrollTop are reset to zero. I only have problem with the controls that are auto postback. Are there solutions? Thanks.
DanYeung
danyeung
Participant
754 Points
369 Posts
Using javascript to maintain the scroll position.
Mar 15, 2007 12:16 PM|LINK
I need to make the page maintain the scroll position on postback. I used the SmartNavigation and that caused lost focus on the field. I found the following javascript from http://aspnet.4guysfromrolla.com/articles/111704-1.aspx. It seems working on the demo in the link, but not working in my page. I am using ASP.NET 2003. Can you please help? I am not sure if the function or the window.onclick and the window.onkeypress does not work. I added the two hidden fields scrollLeft and scrollTop in the form. Thanks.
function SmartScroller_GetCoords()
{
var scrollX, scrollY;
if (document.all)
{
if (!document.documentElement.scrollLeft)
scrollX = document.body.scrollLeft;
else
scrollX = document.documentElement.scrollLeft;
if (!document.documentElement.scrollTop)
scrollY = document.body.scrollTop;
else
scrollY = document.documentElement.scrollTop;
}
else
{
scrollX = window.pageXOffset;
scrollY = window.pageYOffset;
}
document.Form1.scrollLeft.value = scrollX;
document.Form1.scrollTop.value = scrollY;
}
function SmartScroller_Scroll()
{
var x = document.Form1.scrollLeft.value;
var y = document.Form1.scrollTop.value;
window.scrollTo(x, y);
}
window.onload = SmartScroller_Scroll;
window.onscroll = SmartScroller_GetCoords;
window.onclick = SmartScroller_GetCoords;
window.onkeypress = SmartScroller_GetCoords;
<input name="scrollLeft" id="scrollLeft" type="hidden" value="0" />
<input name="scrollTop" id="scrollTop" type="hidden" value="0" />
DanYeung
SGWellens
All-Star
126031 Points
10310 Posts
Moderator
Re: Using javascript to maintain the scroll position.
Mar 15, 2007 03:28 PM|LINK
Have you looked at this property?
Page.MaintainScrollPositionOnPostBack
My blog
JoshStodola
Star
13736 Points
3177 Posts
Re: Using javascript to maintain the scroll position.
Mar 15, 2007 04:44 PM|LINK
Steve: you will see in his post he is using ASP 1.x and this property is not available.
I rewrote that example on 4GuysFromRolla to tailor my needs several months ago.
What you need is:
Step 1:
<input type="hidden" id="scrollLeft" /> <input type="hidden" id="scrollTop" />Step 2: Copy this Javascript code into a *.js file and reference it in your page:
function getCoords() { var scrollX, scrollY; if(document.all) { if(document.documentElement.scrollLeft) scrollX = document.documentElement.scrollLeft; else scrollX = document.body.scrollLeft; if(document.documentElement.scrollTop) scrollY = document.documentElement.scrollTop; else scrollY = document.body.scrollTop; } else { scrollX = window.PageXOffset; scrollY = window.PageYOffset; } document.forms[0].scrollLeft.value = scrollX; document.forms[0].scrollTop.value = scrollY; } function setScrollCoords() { window.scrollTo(document.forms[0].scrollLeft.value, document.forms[0].scrollTop.value); } window.setInterval('getCoords()', 1000);Step 3: And now call it in the onload of the <body>:
Hope this helps! Don't forget to mark the most helpful post(s) as Answer for the sake of future readers. Thanks!
SGWellens
All-Star
126031 Points
10310 Posts
Moderator
Re: Using javascript to maintain the scroll position.
Mar 15, 2007 05:26 PM|LINK
Josh: I did notice his post said ASP.NET 2003. I wasn't aware this implied .NET 1.x
If there was ever a compelling reason to upgrade, this certainly qualifies!
My blog
JoshStodola
Star
13736 Points
3177 Posts
Re: Using javascript to maintain the scroll position.
Mar 15, 2007 08:17 PM|LINK
2003 = 1.x
And a simple automatic script implementation is a sad reason to upgrade! (MaintainScrollPositionOnPostback will pretty much result in the exact script I posted, with two hidden fields, and a javascript call on the body onload). And have you gone through the 2003 -> 2005 conversion process on a large-scale web app? It isn't all that fun, trust me.
danyeung
Participant
754 Points
369 Posts
Re: Using javascript to maintain the scroll position.
Mar 16, 2007 01:39 PM|LINK
The Javascripts ran but the scrollX and scrollY returned zeros always. I tried when I clicked a radio button at the bottom of the form. I checked by adding alert(scrollX); and alert(scrollY) after document.forms[0].scrollTop.value = scrollY; Any idea? Thanks.
DanYeung
JoshStodola
Star
13736 Points
3177 Posts
Re: Using javascript to maintain the scroll position.
Mar 16, 2007 01:46 PM|LINK
It works fine over here in IE7. You said you added an alert box above that line, you should have gotten a popup alert every second. Did you?
Jonathan Dav...
Participant
1048 Points
234 Posts
Re: Using javascript to maintain the scroll position.
Mar 16, 2007 02:36 PM|LINK
danyeung
Participant
754 Points
369 Posts
Re: Using javascript to maintain the scroll position.
Mar 18, 2007 12:02 AM|LINK
(1) I changed the interval to 5000, so I would have enough time to scroll down to the bottom of the form.
(2) Yes, it is in a frame.
I also found out that it works if the control does not have auto postbask. If the control has auto postback, the scrollLeft and scrollTop are reset to zero. I only have problem with the controls that are auto postback. Are there solutions? Thanks.
DanYeung
Haissam
All-Star
37421 Points
5632 Posts
Re: Using javascript to maintain the scroll position.
Mar 18, 2007 01:00 AM|LINK
Take a look into the below link, it might help you
How to Maintain Scroll Position
HC
MCAD.NET
| Blog |