Asp.net

在部分回發 ASP.NET 上保持面板滾動位置

  • March 13, 2011

我有一個放在 ASP.NET 面板中的 gridview。面板和 Gridview 都在 UpdatePanel 中。gridview 中有一個列會導致部分回發。我想在這些回發上保持面板滾動位置。有什麼辦法嗎?問候。

在 asp.net 中沒有內置的工具來解決它

但是,有一個解決此問題的方法;你需要用javascript來處理它。

這裡提到了解決方案:Maintain Scrollbar Position Inside UpdatePanel After Partial 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>

以下是程式碼快照:-

部分回發後保持更新面板內的捲動條位置

引用自:https://stackoverflow.com/questions/5288682