4

I used the following piece of code in the web.config in order to maintain the scrollbar position after a server postback:

<pages maintainScrollPositionOnPostBack="true" >
</pages>

All is working fine, but now i have a gridview encapsuled within a div with a scrollbar in the div (internal scrollbar).

When an event occur on one of the rows inside the gridview, the internal scrollbar doesn't maintain its original position unlike the outer one.

Any ideas?

4

4 回答 4

18

备查:

正常程序是在 web.config 文件中写入以下内容:

  <system.web>
    <pages maintainScrollPositionOnPostBack="true" >
    </pages>
  </system.web>

这将保留所有网页的滚动条位置。

如果您在 gridview(或 div)中有滚动条,则使用以下脚本:

<script type="text/javascript">
    window.onload = function () {
        var strCook = document.cookie;
        if (strCook.indexOf("!~") != 0) {
            var intS = strCook.indexOf("!~");
            var intE = strCook.indexOf("~!");
            var strPos = strCook.substring(intS + 2, intE);
            document.getElementById("grdWithScroll").scrollTop = strPos;
        }
    }
    function SetDivPosition() {
        var intY = document.getElementById("grdWithScroll").scrollTop;
        document.cookie = "yPos=!~" + intY + "~!";
    }
</script>

并且 div 必须如下:

<div id="grdWithScroll" …………  onscroll="SetDivPosition()">

http://michaelsync.net/2006/06/30/maintain-scroll-position-of-div-using-javascript-aspnet-20

于 2012-08-28T07:57:04.637 回答
3

尝试这个,

 <script type="text/javascript">
  window.onload = function () {
               var h = document.getElementById("<%=hfScrollPosition.ClientID%>");
               document.getElementById("<%=scrollArea.ClientID%>").scrollTop = h.value;
              }
   function SetDivPosition() {
           var intY = document.getElementById("<%=scrollArea.ClientID%>").scrollTop;
           var h = document.getElementById("<%=hfScrollPosition.ClientID%>");
           h.value = intY;
     }

  function afterpostback() {

            var h = document.getElementById("<%=hfScrollPosition.ClientID%>");
            document.getElementById("<%=scrollArea.ClientID%>").scrollTop = h.value;

     }

</script> 

 <asp:HiddenField ID="hfScrollPosition" runat="server" Value="0" />
 <div id="scrollArea" onscroll="SetDivPosition()"   runat="server"  style="height:225px;overflow:auto;overflow-x:hidden;"> 

在 Page_Load

if (Page.IsPostBack) {
    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "afterpostback();", true);
    }
于 2013-06-27T07:37:02.007 回答
2

我没有很长的解释和任何解释,最重要的部分是这些代码适用于我的项目。

<script  type="text/javascript">
// This Script is used to maintain Grid Scroll on Partial Postback
var scrollTop;
//Register Begin Request and End Request 
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
//Get The Div Scroll Position

function BeginRequestHandler(sender, args) 
{
var m = document.getElementById('divGrid');
scrollTop=m.scrollTop;
}
//Set The Div Scroll Position
function EndRequestHandler(sender, args)
{
var m = document.getElementById('divGrid');
m.scrollTop = scrollTop;
} 
</script>

这是来自http://www.codeproject.com/Articles/30235/Maintain-GridView-Scroll-Position-and-Header-Insid

于 2016-08-25T05:59:51.603 回答
1

你可以做你想做的事,但它需要在客户端使用 jQuery 之类的东西来完成。以下教程使用 jQuery 确定 GridView 控件中滚动条的值,然后在每次$(document).ready调用该函数时恢复该值。通过这种方式,您的滚动条将根据您的意愿重置到回发之前的位置。

使用 jQuery 在 GridView 中轻松维护滚动位置

于 2012-08-23T13:48:54.177 回答