You shouldnt have changed the interval. The interval of 5000 tells it to save the current scroll position every 5 seconds. If anything, make the number less than 1000. As far as postback, it should work on postback no problem; exact same code works fine
here!!!
I used interval 1000 and it didn't work. So I changed it to 5000 to display the scrollX and scrollY. Do the controls in your form have auto postback set to true?
I use this code on a client's web site that has several grid views on one page so that when they click to sort/page the gridview, their scroll position is retained. When you sort/page a gridview, it is done on postback. Therefore, I know this works with
any postback from anything. That is it's entire purpose!!!!!
You couldn't be putting an alert where you were and get it to display the right values becuse you would never get an opportunity to scroll!
You have to put the onload parameter on teh <body> tag of the page that you want to remember the scroll position. So if you have frames, it wont work if you are using it on the page thta contains the <frameset>!!!!
Try different combinations or something, becuase I can 100% assure you the code I provided works perfectly.
It's really simple (but don't let that fool you into thinking I haven't spent quite a lot of time on this!)
* You trap the post back
* You determine the ID of the element that caused the post-back
* You pop that rendered ID into the little JavaScript function to set the focus and scroll it into view
JoshStodola
Star
13736 Points
3177 Posts
Re: Using javascript to maintain the scroll position.
Mar 18, 2007 03:55 PM|LINK
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Using javascript to maintain the scroll position.
Mar 18, 2007 04:08 PM|LINK
There is a complete solution posted here:
http://forums.asp.net/1201717/ShowPost.aspx
though Josh's solution is essentially the same.
NC...
danyeung
Participant
754 Points
369 Posts
Re: Using javascript to maintain the scroll position.
Mar 18, 2007 10:34 PM|LINK
JoshStodola
Star
13736 Points
3177 Posts
Re: Using javascript to maintain the scroll position.
Mar 18, 2007 10:58 PM|LINK
Listen...
I use this code on a client's web site that has several grid views on one page so that when they click to sort/page the gridview, their scroll position is retained. When you sort/page a gridview, it is done on postback. Therefore, I know this works with any postback from anything. That is it's entire purpose!!!!!
You couldn't be putting an alert where you were and get it to display the right values becuse you would never get an opportunity to scroll!
You have to put the onload parameter on teh <body> tag of the page that you want to remember the scroll position. So if you have frames, it wont work if you are using it on the page thta contains the <frameset>!!!!
Try different combinations or something, becuase I can 100% assure you the code I provided works perfectly.
Bye................
JoshStodola
Star
13736 Points
3177 Posts
Re: Using javascript to maintain the scroll position.
Mar 18, 2007 11:07 PM|LINK
danyeung
Participant
754 Points
369 Posts
Re: Using javascript to maintain the scroll position.
Mar 19, 2007 12:42 PM|LINK
I think using frame caused the problem. It is an existing application. I am adding on new form. Is there a solution? Thanks.
DanYeung
JoshStodola
Star
13736 Points
3177 Posts
Re: Using javascript to maintain the scroll position.
Mar 19, 2007 01:26 PM|LINK
Put an alert in the SetScrollCoords() function and tell me if you ever see it.
If you do, post all of your source code so I can try to find your problem. If not, you didn't follow my example.
Sohnee
Contributor
2560 Points
492 Posts
Re: Using javascript to maintain the scroll position.
Mar 19, 2007 04:26 PM|LINK
Here's what I used to do in 1.1 ... I originally posted this at: http://www.jsworkshop.com/bb/viewtopic.php?t=2232
In your Global.asax page, pop the following script to catch the postback event and take the appropriate action...
_______________________________________________________________________________________________________________________
public static void SetFocus(Page webPage)
{
string controlId = string.Empty;
string serverControlId = string.Empty;
string[] postBackControl = webPage.Page.Request.Form.GetValues("__EVENTTARGET");
if (postBackControl != null && postBackControl.Length > 0)
{
controlId = postBackControl[0];
serverControlId = controlId;
if (webPage.Page.FindControl(controlId) is Calendar)
{
controlId = webPage.Page.FindControl(controlId).ID;
}
}
if (controlId.Length == 0)
{
controlId =webPage.Request.Form.Keys[webPage.Request.Form.Keys.Count-1].ToString();
serverControlId = controlId;
}
if (!webPage.FindControl(serverControlId).Visible)
{
controlId = string.Empty;
}
if (controlId.Length > 0)
{
string javaScript = "<script language=\"JavaScript\">" +
"if (document.getElementById(\"" + controlId + "\") != null) " +
"document.getElementById(\"" +
controlId + "\").focus(); document.getElementById(\"" + controlId + "\").scrollIntoView(true);" +
"</script>";
webPage.Page.RegisterStartupScript("fnScrollToControl", javaScript);
}
}
Then you put this pre-render method on your .aspx page:
{
if(IsPostBack) Global.SetFocus(this);
}
How it works.
It's really simple (but don't let that fool you into thinking I haven't spent quite a lot of time on this!)
* You trap the post back
* You determine the ID of the element that caused the post-back
* You pop that rendered ID into the little JavaScript function to set the focus and scroll it into view
JoshStodola
Star
13736 Points
3177 Posts
Re: Using javascript to maintain the scroll position.
Mar 19, 2007 06:32 PM|LINK
Sohnee,
That looks nice but how well does it work in all browsers?
danyeung
Participant
754 Points
369 Posts
Re: Using javascript to maintain the scroll position.
Mar 20, 2007 03:59 AM|LINK