Here is nice technique that I just implemented.
http://www.wintellect.com/resources/faqs/default.aspx?faq_id=1&page=7
I put it in my Base Page so that every page that needs this
functionality
has it if I add the HTML:
<input type="hidden" name="__SCROLLPOS" value="" />
I tried that as it looks like a pretty good solution.
The problem is I get the following error:
***************************************************************************************
Compiler Error Message: BC30311: Value of type 'System.Web.HttpRequest'
cannot be converted to 'Boolean'.
Source Error:
Line 198:</head>
Line 199:<%
Line 200: if (Request["__SCROLLPOS"] != null &&
Line 201: Request["__SCROLLPOS"] != String.Empty) {
Line 202: int pos = Convert.ToInt32 (Request["__SCROLLPOS"]);
***************************************************************************************
Here is the piece I just cut out of the linked page put in place of the
<body> statement:
****************************************************************************************
<%
if (Request["__SCROLLPOS"] != null &&
Request["__SCROLLPOS"] != String.Empty) {
int pos = Convert.ToInt32 (Request["__SCROLLPOS"]);
Response.Write ("<body id=\"theBody\" "
"onscroll=\"javascript:document.forms[0].__SCROLLPOS.value = "
"theBody.scrollTop;\" "
"onload=\"javascript:theBody.scrollTop=" pos ";\">");
}
else {
Response.Write ("<body id=\"theBody\" "
"onscroll=\"javascript:document.forms[0].__SCROLLPOS.value ="
"theBody.scrollTop;\">");
}
%>
*****************************************************************************************
and here is the hidden object which I put right after the <form
runat="server"> line:
<input type="hidden" name="__SCROLLPOS" value="" />
Am I missing something?
Thanks,
Tom
============================================================================
Is it possible to prevent a Web form from scrolling to the top of the
page
when it posts back to the server?
============================================================================
One way to do it is to add a SmartNavigation="true" attribute to the
page's
@ Page directive. That requires Internet Explorer 5.0 or higher on the
client.
============================================================================
To prevent unwanted scrolling in a wider range of browsers, you can use
a
server-side script that generates client-side script.
The first step is to replace the page's <body> tag with the following
statements:
============================================================================
<%
if (Request["__SCROLLPOS"] != null &&
Request["__SCROLLPOS"] != String.Empty) {
int pos = Convert.ToInt32 (Request["__SCROLLPOS"]);
Response.Write ("<body id=\"theBody\" " +
"onscroll=\"javascript:document.forms[0].__SCROLLPOS.value = "
+
"theBody.scrollTop;\" " +
"onload=\"javascript:theBody.scrollTop=" + pos + ";\">");
}
else {
Response.Write ("<body id=\"theBody\" " +
"onscroll=\"javascript:document.forms[0].__SCROLLPOS.value ="
+
"theBody.scrollTop;\">");
}
%>
============================================================================
Step two is to add the following line somewhere between the <form
runat="server"> and </form> tags:
============================================================================
<input type="hidden" name="__SCROLLPOS" value="" />
============================================================================
How does it work? The server-side script block outputs a <body> tag
containing an onscroll attribute that keeps tabs on the scroll position
and
an onload attribute that restores the last scroll position following a
postback. The scroll position is transmitted to the server in a hidden
<input> control named __SCROLLPOS. Note that this technique is
compatible
with Internet Explorer but not with Netscape Navigator.
============================================================================
--
Joe Fallon
I have some nested DataGrids in my Datalist. There could be 20
datalist items.
If I scroll down to the 18th item and expand it to show the DataGrid,
the page always goes back to the beginning of the page instead of
going back to the same spot on the page or move the page up to show
the grid.
Is there a way to have it go back to the place I left on the page?
Thanks,
Tom