【问题标题】:Scroll to previous place after postback in ASP.NET WebForms在 ASP.NET WebForms 中回发后滚动到上一个位置
【发布时间】:2014-01-14 18:26:39
【问题描述】:

我正在使用 ASP.NET WebForms 实现跨浏览器 Web 应用程序。因此,我动态创建了表格,其中每个单元格都是从TableCell 继承的自定义用户控件。在我的控件中有 asp:Panel 和几个文本框。由于表格的动态创建,它可以是不同的宽度。当我在其中一个文本框中更改文本时,TextChanged 事件会触发并刷新页面。但因为它滚动跳转到顶部。如何将页面滚动到上一个位置?

我试过Page.MaintainScrollPositionOnPostBack = true,但它不起作用。

谢谢!

【问题讨论】:

    标签: c# javascript jquery asp.net webforms


    【解决方案1】:

    您需要使用 JavaScript 来协助您完成这项工作。几年前,我遇到了同样的问题,并且能够在回发后使用 JavaScript 进行修复。

    这里有一篇文章供你阅读:http://basgun.wordpress.com/2008/06/09/maintain-scroll-position-updatepanel-postback/

    基本上,您将向您的页面请求添加事件和参数,这将为您完成提升。

    文章代码:

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

    【讨论】:

      猜你喜欢
      • 2010-10-11
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 2013-07-17
      • 2019-08-03
      • 1970-01-01
      • 2013-03-25
      相关资源
      最近更新 更多