在部分回发ASP.NET上维护面板滚动位置

时间:2022-08-24 03:38:27

I have a gridview that putted in ASP.NET Panel. both of panel and Gridview are in an UpdatePanel. there is a column in gridview that Causes Partial PostBacks. i want to Maintain Panel Scroll position on those postbacks. Is there any way? regards.

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

2 个解决方案

#1


39  

There is no built-in facility to resolve it in asp.net

在asp.net中没有可以解决它的内置工具

However, there is a workaround for this problem; You need to handle it with javascript.

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

Solution is mentioned here: Maintain Scrollbar Position Inside UpdatePanel After Partial PostBack

这里提到了解决方案:在部分PostBack之后维护UpdatePanel内的滚动条位置

Edited 20-May-2012; after seeing the comments

<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>

Below is the code snapshot:-

以下是代码快照: -

在部分回发ASP.NET上维护面板滚动位置

#2


5  

Add MaintainScrollPositionOnPostback="true" to your page directive.

将MaintainScrollPositionOnPostback =“true”添加到页面指令中。

#1


39  

There is no built-in facility to resolve it in asp.net

在asp.net中没有可以解决它的内置工具

However, there is a workaround for this problem; You need to handle it with javascript.

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

Solution is mentioned here: Maintain Scrollbar Position Inside UpdatePanel After Partial PostBack

这里提到了解决方案:在部分PostBack之后维护UpdatePanel内的滚动条位置

Edited 20-May-2012; after seeing the comments

<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>

Below is the code snapshot:-

以下是代码快照: -

在部分回发ASP.NET上维护面板滚动位置

#2


5  

Add MaintainScrollPositionOnPostback="true" to your page directive.

将MaintainScrollPositionOnPostback =“true”添加到页面指令中。