在部分回发 ASP.NET 上保持面板滚动位置

2024-03-16

我有一个放在 ASP.NET 面板中的 gridview。 panel 和 Gridview 都在 UpdatePanel 中。 gridview 中有一个列会导致部分回发。 我想在这些回发上保持面板滚动位置。 有什么办法吗? 问候。


ASP.NET 中没有内置的工具来解决这个问题

不过,这个问题有一个解决方法;你需要用javascript来处理它。

这里提到了解决方案:部分回发后保持 UpdatePanel 内滚动条的位置 http://basgun.wordpress.com/2008/06/09/maintain-scroll-position-updatepanel-postback/

2012 年 5 月 20 日编辑;看到评论后

<form id="form1" runat="server">
  <asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Release" />
   <script type="text/javascript">
      // It is important to place this JavaScript code after ScriptManager1
      var xPos, yPos;
      var prm = Sys.WebForms.PageRequestManager.getInstance();

      function BeginRequestHandler(sender, args) {
        if ($get('<%=Panel1.ClientID%>') != null) {
          // Get X and Y positions of scrollbar before the partial postback
          xPos = $get('<%=Panel1.ClientID%>').scrollLeft;
          yPos = $get('<%=Panel1.ClientID%>').scrollTop;
        }
     }

     function EndRequestHandler(sender, args) {
         if ($get('<%=Panel1.ClientID%>') != null) {
           // Set X and Y positions back to the scrollbar
           // after partial postback
           $get('<%=Panel1.ClientID%>').scrollLeft = xPos;
           $get('<%=Panel1.ClientID%>').scrollTop = yPos;
         }
     }

     prm.add_beginRequest(BeginRequestHandler);
     prm.add_endRequest(EndRequestHandler);
 </script>

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
     <asp:Panel ID="Panel1" runat="server" Height="300">
        <%-- Some stuff which would cause a partial postback goes here --%>
     </asp:Panel>
   </ContentTemplate>
 </asp:UpdatePanel>

</form>

以下是代码快照:-

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在部分回发 ASP.NET 上保持面板滚动位置 的相关文章

随机推荐