在PostBack上保持滚动位置

时间:2017-12-15 17:18:13

标签: javascript c# asp.net scroll postback

我使用Visaul Studio Community 15.5.2与WebForm和C#

一些建议

<%@ Page MaintainScrollPositionOnPostBack="true" .....

和web.config

 <pages maintainScrollPositionOnPostBack="true" ></pages>

我试过了,但是即使没有脚本,也没有浏览器(包括Edge),maintainScrollPositionOnPostBack没有效果

此代码仅使用一个DIV

<script type="text/javascript"> 
window.onload = function () {
    var div = document.getElementById("dvScroll");
    var div_position = document.getElementById("div_position");
    var position = parseInt('<%=!string.IsNullOrEmpty(Request.Form["div_position"]) ? Request.Form["div_position"] : "0" %>');
        div.scrollTop = position;
        div.onscroll = function () {
            document.getElementById("div_position").value = div.scrollTop;
        };
};
  </script> 
带有HiddenField的DIV,一切都很完美。

 <form id="form1" runat="server">
    <div id="dvScroll"; style="width:450px; height:300px; overflow:scroll; float:left">
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CellPadding="4">
          <Columns>         
            <asp:BoundField DataField="ID_NAVE" HeaderText="IdNave" ItemStyle-Width="50" ItemStyle-wrap="false" Visible="true" />
            <asp:BoundField DataField="NOMENAVE" HeaderText="nave" ItemStyle-Width="15" ItemStyle-wrap="false" Visible="true" />
          </Columns>
        </asp:GridView>
    </div>
    <input type="hidden" id="div_position" name="div_position" />

但是使用第二个DIV代码不适用(window.onload = function()是唯一的,修改脚本以接受DIV作为参数会很好,但我不知道JS < / p>

<div id="dvScroll2"; style="clear:both; width:600px; height:300px; overflow:scroll;">       
       <asp:HiddenField ID="HiddenField2" runat="server" />
       <asp:GridView ID="GridMnuRest" runat="server" AutoGenerateColumns="false" CellPadding="4" OnRowCreated="GridMnuRest_RowCreated" OnRowDataBound="GridMnuRest_RowDataBound" OnSelectedIndexChanged="GridMnuRest_SelectedIndexChanged" OnSelectedIndexChanging="GridMnuRest_SelectedIndexChanging" OnRowCommand="GridMnuRest_RowCommand">                            
           <Columns>       
            <asp:TemplateField >
              <ItemTemplate>
                <asp:CheckBox ID="MnuSelect" runat="server" AutoPostBack="true" OnCheckedChanged="GridMnuRest_OnCheckedChanged" />
              </ItemTemplate>
            </asp:TemplateField>                  
           <asp:BoundField DataField="id_ristorante" HeaderText="IdRst" ItemStyle-Width="20" ItemStyle-wrap="false" Visible="true" />
           <asp:BoundField DataField="description" HeaderText="Description" ItemStyle-Width="300" ItemStyle-wrap="false" Visible="true" />
          </Columns>
       </asp:GridView>
   </div>
   <input type="hidden" id="div_position2" name="div_position2" />

即使我创建了一个包含已更改变量的新脚本

<script type="text/javascript"> 
window.onload = function () {
    var div2 = document.getElementById("dvScroll2");
    var div_position2 = document.getElementById("div_position2");
    var position2 = parseInt('<%=!string.IsNullOrEmpty(Request.Form["div_position2"]) ? Request.Form["div_position2"] : "0" %>');
        div2.scrollTop = position2;
        div2.onscroll = function () {
            document.getElementById("div_position2").value = div2.scrollTop;
        };
};

0 个答案:

没有答案