简体   繁体   English

在带有母版页的 div 中滚动 position

[英]scroll position in div with master page

i have a div tag within a content tag as i am using a masterpage which has the forms and body tags.我在内容标签中有一个 div 标签,因为我正在使用具有 forms 和正文标签的母版页。

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">

  <div id="xxx"  style="overflow:scroll; height:450px;">

     <asp:GridView ID="GridView1" runat="server" ...>

     </asp:GridView>

  </div>
</Content>

I wish to maintain the scroll position of the div when any postback happens.我希望在发生任何回发时保持 div 的滚动 position 。

There are jscripts when i search for it but i dont know how do i apply them with the masterpage.当我搜索它时有 jscripts,但我不知道如何将它们与母版页一起应用。

please help if there is an easier way.如果有更简单的方法,请提供帮助。 or how to use javascript with the above code.或者如何在上面的代码中使用 javascript。

Any help is appreciated.. thanks任何帮助表示赞赏..谢谢

You can use AJAX to remove postbacks which will keep your position.您可以使用 AJAX 删除回发,这将保留您的 position。 More specifically you can take a look at the UpdatePanel control.更具体地说,您可以查看 UpdatePanel 控件。

Update:更新:

A non-AJAX solution would be to add the following attribute in your page tag.非 AJAX 解决方案是在您的页面标记中添加以下属性。

<%@ Page MaintainScrollPositionOnPostback="true" %> <%@ Page MaintainScrollPositionOnPostback="true" %>

Try this it worked for me.试试这个它对我有用。

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    <div id="divScroll" style="overflow:auto; height:450px;">  
        <asp:GridView ID="GridView1" runat="server" ...>
        </asp:GridView>   
    </div> 
</Content> 
<script type="text/javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);               
</script>
</asp:UpdatePanel>

javascript file add the following code: javascript 文件添加以下代码:

var scrollTop;
function BeginRequestHandler(sender, args) 
{
    var m = document.getElementById('divScroll');
    scrollTop = m.scrollTop;
}
function EndRequestHandler(sender, args) {
    var m = document.getElementById('divGrid');
    m.scrollTop = scrollTop;
}

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

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