简体   繁体   English

在ASP.NET中的div中维护asp:table滚动位置

[英]Maintaining asp:table Scroll Position inside a div in ASP.NET

I have an asp:Table inside a div. 我在div内有一个asp:Table。 How do I maintain its scroll position on every postback? 如何在每次回发中保持其滚动位置?

<div style="overflow: visible">
    <asp:Table ID="ReportBody" runat="server" EnableViewState="False" CellSpacing="0">
    </asp:Table>
</div>

I tried MaintainScrollPositionOnPostback=true in the Page directive to no avail. 我在Page指令中尝试过MaintenanceScrollPositionOnPostback = true,但无济于事。 I also tried Page.MaintainScrollPositionOnPostback = true in PageLoad and didn't work. 我还在PageLoad中尝试了Page.MaintainScrollPositionOnPostback = true,但没有用。 I also tried a bunch of JS scripts from Stackoverflow and nothing worked. 我还尝试了来自Stackoverflow的一堆JS脚本,但没有任何效果。

Thanks. 谢谢。

It's a little difficult to answer specifically, as I don't get much sense of a page structure from what's provided, but as a broad guess, I'd say that I would combine jQuery and ASP.NET for this -- using jQuery to track the scroll, write it to a .NET input hidden field, and then reapply it whenever the page reloads. 具体来说,要回答这个问题有点困难,因为我对所提供的内容没有太多的了解,但是作为一个广泛的猜测,我会说我将结合使用jQuery和ASP.NET -使用jQuery来跟踪滚动,将其写入.NET输入隐藏字段,然后在页面重新加载时重新应用它。

You may need to adjust this to fit the needs of your actual document, but I'm thinking of something along these lines. 您可能需要调整它以适合实际文档的需求,但是我正在考虑遵循这些思路。 (Note: if you want a scroll within the page , rather than within a specific div, refer to the notes at the end of this answer.) (注意:如果要在页面中滚动而不是在特定的div中滚动,请参阅此答案末尾的注释。)

Firstly, for the main div, I'm assuming you have something like: 首先,对于主要div,我假设您有以下内容:

<div id="container" style="overflow-y:auto;">

...so that the content of the div can scroll vertically. ...以便div的内容可以垂直滚动。 Working with that assumption, the next addition would be a hidden field within the page: 使用该假设,下一个添加项将是页面内的隐藏字段:

<asp:HiddenField ID="tScroll" runat="server" value="0" />

The default of 0 is there for when the page first loads. 页面首次加载时的默认值是0。 Lastly, you use jQuery to both track the scroll position, and to reapply the tracked scroll position whenever the page is reloaded and the document ready step is triggered: 最后,无论何时重新加载页面并触发文档准备步骤,您都使用jQuery跟踪滚动位置并重新应用跟踪的滚动位置:

$(document).ready(function() {
    $("#container").scrollTop($("#tScroll").val()); // this sets the scroll position
    $("#container").scroll(function() {
        $("#tScroll").val($(this).scrollTop());
    });
});

However, if the div and table are visible when the page loads, you'll get a split-second jerk as the div appears initially in its normal position, and then jumps to the scrolled position. 但是,如果在页面加载时div和表可见 ,则div最初会显示在其正常位置,然后会跳到滚动位置,这会导致瞬间跳动。 Instead, you can perform a smooth scroll back by using the jQuery animate function, and replace that line with: 相反,您可以使用jQuery animate函数执行平滑的向后滚动,并将该行替换为:

$("#container").animate({ scrollTop: $("#tScroll").val() + 'px' }, 500);

...adjusting the 500 to be larger or smaller as you wish. ...根据需要将500调整为更大或更小。 This way, the div will scroll itself back to the position you need. 这样,div将自身滚动回到所需位置。

I can't tell if this is an exact fit for what you need, but hopefully it should at least point you in the right direction. 我不能说这是否完全适合您的需求,但希望它至少可以为您指明正确的方向。

Addendum to this: if you're looking to maintain a page scroll, rather than a scroll within a specific div, the logic is identical, but you'd be tracking and referencing a couple of different values, essentially as follows: 附录:如果要维护页面滚动而不是特定div中的滚动,逻辑是相同的,但是您将跟踪并引用几个不同的值,基本上如下:

$('html,body').animate({ scrollTop: $("#tScroll").val() }, 500);
$(window).scroll(function(e) { 
    ...

...with the rest of the script kept in the same line. ...其余脚本保持在同一行。 You can choose whether to use the animate or the direct scrollTop assignment in either case. 在两种情况下,您都可以选择使用动画还是直接使用scrollTop分配。

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

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