|
 |
aspx thread: Postbacks and Page positioning
Message #1 by "Whitney Andrew" <whitneyandrew@a...> on Thu, 19 Dec 2002 15:49:31
|
|
When a control causes a postback on a page that has vertical scrolling,
how can you cleanly have the page repost to the point where (or in the
general area of where) the user was when they clicked the control causing
the postback?
The method I'm using is to (when the event fires that causes the postback)
add a client side function to the onload event of the body tag that uses
the ScrollTo method of the Window object to scroll the page to an anchor
tag by that control.
private void btnMyButton_Click(object sender, System.EventArgs e) {
htmlBodyTag.Attributes.Add("onLoad", "window.scrollTo(0,
document.anchors.item('ancMyAnchor').offsetParent.offsetTop);");
MoreCode;
}
This doesn't look so clean upon presentation. The page renders with the
window scrolled all the way to the top and then jerks down to that spot.
I was hoping to have a solution that works more like a bookmark to an
anchor tag passed in the URL
(http://www.mydomain.com/mypage.aspx#bookmark). Is there a way to
manipulate the url of the postback or are there any other suggestions?
Thanks.
Message #2 by "Peter Lanoie" <planoie@n...> on Thu, 19 Dec 2002 11:45:56 -0500
|
|
Well, the problem of the scrolling jerk is unavoidable from the way I see
it.
Perhaps you can add a client side script call that modifies the form action
to include that anchor...
(Just making this up, you'll have to debug it. ;)
function(strAnchor){
// Probably want to add something that strips any existing anchor off
first...
document.forms[0].action += "#" + strAnchor
}
You can call this from all your postback trigger objects (buttons, images,
etc) on the form (add the function call to the attributes collection
"onClick" key), and include the correct anchors in the html(and function
call). Of course, if you don't add that anchor strip bit in, you'll end up
with a long list of anchors on the end of your URL..
...aspx#anchor1#anchor2
Other side affect is that the page will still jerk a bit, cause the
likelihood of the browser being set perfectly on that anchor before the
refresh is pretty slim.
Ah, yet another con to the postback model...
Cheers!
Peter
-----Original Message-----
From: Whitney Andrew [mailto:whitneyandrew@a...]
Sent: Thursday, December 19, 2002 15:50
To: ASP.NET
Subject: [aspx] Postbacks and Page positioning
When a control causes a postback on a page that has vertical scrolling,
how can you cleanly have the page repost to the point where (or in the
general area of where) the user was when they clicked the control causing
the postback?
The method I'm using is to (when the event fires that causes the postback)
add a client side function to the onload event of the body tag that uses
the ScrollTo method of the Window object to scroll the page to an anchor
tag by that control.
private void btnMyButton_Click(object sender, System.EventArgs e) {
htmlBodyTag.Attributes.Add("onLoad", "window.scrollTo(0,
document.anchors.item('ancMyAnchor').offsetParent.offsetTop);");
MoreCode;
}
This doesn't look so clean upon presentation. The page renders with the
window scrolled all the way to the top and then jerks down to that spot.
I was hoping to have a solution that works more like a bookmark to an
anchor tag passed in the URL
(http://www.mydomain.com/mypage.aspx#bookmark). Is there a way to
manipulate the url of the postback or are there any other suggestions?
Thanks.
|
|
 |