In this FAQ, you will see references to a value called 'sortVal'. The only reason I'm including this is to try to make this FAQ more "real world" and put it into context. I don't do anything with the value, and only include it for the aforementioned reason. Don't get confused or bogged down in it. It's secondary to our purposes here -- which is why I comment out where you might (or might not) want to use it.
So let's draw a quick picture... you have an ASP page which displays some information in a table, and that table is sortable by clicking on any of the header cells in that table.
Problem is, when a user clicks to sort, and your page reloads itself, the page loads back up at a scroll position of 0 (the top of the page), and the continuity of your application is sort of lost right there because the user has to scroll back down and find what they were looking at. Isn't it funny what little things can just ruin a nice application?
Well, luckily, there is a property which you can use to alleviate this problem and set the page right back where you started:
and it's a client side property.
Now, for this example, I'm going to make an assumption:
The functionality of your page is built using a recursive form (one that submits to itself) and is using a "method" of post. For instance, the page in question is called 'display.asp', then your form tag would look like this: <form name=theForm method=post action=display.asp>
One more side note before we get started... document.body.scrolltop is a strange bird. It is both a property and a method (command and query), which makes it easy to work with, but sort of confusing at the same time. No worries, though. Here's how we'll work it.
This process uses three pieces:
So, the first thing you need to do to your page to set this up is to add this hidden form variable to the end of your form, and it looks like this:
<input type=hidden name=thePosition>
That's the form element that will be set in order to send the value along to the next load of the page -- which you will use to move the page to the desired location.
//now, you need to submit your form: document.theForm.submit(); } </script>
Ok, next, you'll need to write some ASP that will retrieve the value from your form object like this:
<% dim thePosition thePosition = request.form("thePosition") %>
And finally, if there's a value in your ASP variable, you'll need to write this bit of code that will actually do the work of moving your page to its location. Put it right before your closing body tag (</body>)
So that it won't get written to the screen unless there is a value in your ASP variable.
Was that chopped up enough for you? Here's the whole thing again in a working example that you can play with -- minus the narration so you can see how the whole thing flows:
<%@language=vbscript%> <%option explicit%> <% dim thePosition thePosition = request.form("thePosition")
'Here's where you might want to grab the 'sortVal' -- ' assuming you assigned it to some other hidden form ' variable, you could grab it here and tack it onto the ' end of your SQL Statement that you use to grab your ' data from the db. Put it in your ORDER BY clause to ' resort your data by the desired criteria. And if you ' really want to get fancy, keep a sort direction stored ' too -- so that if they click the same cell, it sorts in ' the opposite direction. But I digress.