简体   繁体   English

在updatepanel部分回发后维护页面滚动位置

[英]Maintaining page scroll position after updatepanel partial postback

I am a beginner at ASP.NET and I have a problem maintaining the scroll position of the page after a partial postback of an UpdatePanel. 我是ASP.NET的初学者,在部分回发UpdatePanel后,我在维护页面的滚动位置时遇到问题。 I tried setting MaintainScrollPositionOnPostback="true" in <%@ Page Language="C#" ...%> but it didn't do the trick. 我尝试在<%@ Page Language="C#" ...%>设置MaintainScrollPositionOnPostback="true" ,但它没有做到这一点。 Please note that I am using (and have to use) FireFox. 请注意我正在使用(并且必须使用)FireFox。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you! 谢谢! Here is my code: 这是我的代码:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:HiddenField ID="ClassificationHiddenField" runat="server" />
<asp:HiddenField ID="DateHiddenField" runat="server" />
<table>
    <tr>
        <td>
            <asp:Panel ID="GroupTitlePanel" CssClass="titlePanelBold" BorderStyle="Ridge" runat="server"
                Width="400px">
                <table id="MainTable">
                    <tr>
                        <td align="center" class="style3">
                            <asp:Label ID="GroupLabel" runat="server">
                            </asp:Label>
                        </td>
                        <td align="center" class="style4">
                            <asp:Label ID="ReturnLabel" runat="server" Text="Expected Return">
                            </asp:Label>
                        </td>
                    </tr>
                </table>
            </asp:Panel>
            <br />
            <asp:Panel ID="GroupMainPanel" runat="server" Width="400px">
            </asp:Panel>
        </td>
        <td width='100px'>
        </td>
        <td>
        </td>
    </tr>
</table>
<asp:Panel ID="BottomPanel" runat="server" BorderStyle="Ridge">
    <table>
        <tr>
            <td align="center">
                <br />
                <asp:ToolkitScriptManager ID="ToolkitScriptManager1" EnablePartialRendering="true"
                    runat="server">
                </asp:ToolkitScriptManager>
                <asp:CheckBoxList runat="server" ID="GroupCheckBoxList" RepeatColumns="10" RepeatDirection="Horizontal"
                    RepeatLayout="Table" AutoPostBack="true" ClientIDMode="AutoID" OnSelectedIndexChanged="GroupCheckBoxList_SelectedIndexChanged">
                </asp:CheckBoxList>
            </td>
        </tr>
        <tr>
            <td>
                <asp:UpdatePanel ID="GroupUpdatePanel" runat="server" Visible="true" UpdateMode="conditional">
                    <ContentTemplate>
                        <asp:Panel ID="GroupGraphPanel" runat="server" Visible="true">
                        </asp:Panel>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="GroupCheckBoxList" EventName="SelectedIndexChanged" />
                    </Triggers>
                </asp:UpdatePanel>
            </td>
        </tr>
    </table>
</asp:Panel>

This looks like the answer to your question. 这看起来像是你问题的答案。 As a plus; 作为一个加号; it appears to work on every browser not just FF. 它似乎适用于每个浏览器而不仅仅是FF。

http://www.c-sharpcorner.com/Blogs/11804/maintain-scroll-position-on-postback-within-updatepanel.aspx http://www.c-sharpcorner.com/Blogs/11804/maintain-scroll-position-on-postback-within-updatepanel.aspx

if you are using IE then its very simple just put the code in your page directive. 如果您使用的是IE,那么只需将代码放在您的页面指令中即可。

<%@ Page Language="C#" AutoEventWireup="true" 
 CodeFile="Default.aspx.cs" Inherits="_Default"
 MaintainScrollPositionOnPostback="true" %> 

but it will not work in Firefox for that you have to add one browser file into your website 但它不适用于Firefox,因为您必须在网站中添加一个浏览器文件

Right click on solution explorer > Add New Item 右键单击解决方案资源管理器>添加新项

Select Browser File and add it to App_Browsers folder. 选择“浏览器文件”并将其添加到App_Browsers文件夹。

Add MaintainScrollPositionOnPostback capability to this browser file as written below. 将MaintainScrollPositionOnPostback功能添加到此浏览器文件,如下所示。

<browsers>
  <browser refID="Mozilla">
      <capabilities>
        <capability name="supportsMaintainScrollPositionOnPostback" value="true" />       
    </capabilities>   
  </browser>
</browsers>

Some times this also not work, 有时这也行不通,

Then a simple solution just add a blank Update panel after the grid and onpostback just put the focus to that update panel it will work in any browser. 然后一个简单的解决方案只需在网格和onpostback之后添加一个空白的Update面板,只需将焦点放在该更新面板上即可在任何浏览器中使用。 in cs postbackevent updatepanel1.Focus(); 在cs postbackevent updatepanel1.Focus();

If any problem just feel free to ask or any modification reply. 如有任何问题,请随时提出或任何修改回复。

Though I understand that you are not familiar with javascript, still i'm suggesting this answer to you as there is no inbuilt solution for this in .net but you can achieve it with javascript with a work around. 虽然我知道你不熟悉javascript,但我仍然建议给你这个答案,因为在.net中没有内置的解决方案,但是你可以用javascript来解决它。 Don't worry Javascript ain't tough and is one of the important part of web development. 不要担心Javascript不强硬,是Web开发的重要组成部分之一。 So just give it a try. 所以试一试吧。 Might help you. 可能会帮助你。

You can Refer to this Page : Maintaining page scroll position after updatepanel partial postback 您可以参考本页: 在updatepanel部分回发后维护页面滚动位置

<form id="form1" runat="server">
  <asp:ScriptManager ID="SM1" 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>

You can set focus on the control you'd like to see on the screen. 您可以将焦点设置在您想要在屏幕上看到的控件上。

eg if dropdownlist "ddlCity" is the control that causes the postback, then do the following after your dropdownlist SelectedIndexChanged code: 例如,如果dropdownlist“ddlCity”是导致回发的控件,则在下拉列表SelectedIndexChanged代码后执行以下操作:

ddlCity.Focus(); ddlCity.Focus();

I was able to resolve a similar problem with the following hack: 我能够用以下hack解决类似的问题:

Add HiddenField Control to the page or control you're working in. Be sure to set the ClientIDMode to static so that it is easily accessible in JavaScript. 将HiddenField Control添加到您正在使用的页面或控件中。确保将ClientIDMode设置为静态,以便可以在JavaScript中轻松访问它。 We will use JavaScript to update this control: 我们将使用JavaScript来更新此控件:

<asp:HiddenField ID="scrollPosition" ClientIDMode="Static" runat="server" /> 

Also Add a panel control as the target to which we will insert some javascript: 还添加一个面板控件作为我们将插入一些javascript的目标:

<asp:Panel ID="pnlScriptRunner" runat="server"></asp:Panel>

Add the following JavaScript. 添加以下JavaScript。 With the window.onscroll function, we are updating our HiddenField Control. 使用window.onscroll函数,我们正在更新我们的HiddenField控件。 The updateScrollPosition function will be called from our C# code behind: updateScrollPosition函数将从我们的C#代码后面调用:

<script> 
     window.onscroll = function () {
          var ctrl = document.getElementById("scrollPosition");
          ctrl.value = document.body.scrollTop;
          console.log(ctrl.value);
     };

     function updateScrollPosition(value) {       
         window.scrollTo(0, value);
         console.log("updating scroll position");
     }
</script> 

Create a new C# Class and add the following method. 创建一个新的C#类并添加以下方法。 This will allow us to insert some Javascript from the code-behind in C#: 这将允许我们从C#中的代码隐藏中插入一些Javascript:

public static class ClientScript
{
    public static void InsertScript(string script, Control target)
    {
        HtmlGenericControl s = new HtmlGenericControl();
        s.TagName = "script";
        s.InnerHtml = script;
        target.Controls.Add(s); 
    }        
}

Now, in the code behind of your control or page, call the JavaScript function "updateScrollPosition(value)" with the value from our ASP.NET HiddenField Control by inserting the javascript into pnlScriptRunner with the static class we created: 现在,在您的控件或页面的代码中,通过将javascript插入到我们创建的静态类的pnlScriptRunner中,使用ASP.NET HiddenField Control中的值调用JavaScript函数“updateScrollPosition(value)”:

    protected void btnRotate_Click(object sender, EventArgs e)
    {
         //Do stuff with controls in your update panel here, then: 
         ClientScript.InsertScript("updateScrollPosition(" + scrollPosition.Value + ");", pnlScriptRunner);
         UpdatePanel1.Update();

    }

My btnRotate_Click event is registered as a trigger in the update panel: 我的btnRotate_Click事件在更新面板中注册为触发器:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
     <ContentTemplate>
         <b>Image Preview: </b><br />
         <asp:Image ID="img" runat="server" CssClass="profileImage" />
         <br /> 
         <br />
         <asp:Button ID="btnRotate" runat="server" Text="Rotate Image" ClientIDMode="Static" OnClick="btnRotate_Click" />
         <br />
         <br />
      </ContentTemplate>
      <Triggers>
           <asp:PostBackTrigger ControlID="btnRotate" />
      </Triggers>
</asp:UpdatePanel>

The following references are necessary: 以下参考是必要的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

Hopefully this helps! 希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM