简体   繁体   English

ASP.NET在数据列表和用户控件内的div中回发后保持滚动位置

[英]ASP.NET Maintain scroll position after postback in div inside data list and user control

My situation looks like this: I've got x-axis scrollable div that contains buttons with numbers of page displayed. 我的情况看起来像这样:我有x轴可滚动的div,其中包含带有显示的页面编号的按钮。 This div is placed in data list inside User Control responsible for displaying news on my site. 该div放置在负责在我的网站上显示新闻的用户控件内的数据列表中。

This is the code of this div and datalist with page numbers. 这是此div和带有页码的数据列表的代码。

<div style="width:430px; overflow:auto; overflow-y:hidden; -ms-overflow-y:hidden; vertical-align:top; position:relative; top:-1px; ">
            <asp:DataList ID="dlPaging" runat="server" OnItemCommand="dlPaging_ItemCommand" RepeatDirection="Horizontal" 
                OnItemDataBound="dlPaging_ItemDataBound">
                <ItemTemplate>
                    <asp:Button ID="lnkbtnPaging" class="pagebutton" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                        CommandName="lnkbtnPaging" Text='<%# Eval("PageText") %>' CausesValidation="False" />
                </ItemTemplate>
            </asp:DataList>
            </div>

How to mantain x-axis position of this div after postback? 回发后如何保持该div的x轴位置? I've tried several tricks, javascript and I can't figure it out. 我尝试了几种技巧,而javascript却无法弄清楚。

<div id="divDtPaging" runat="server" visible="true" style="width: 50%; overflow: scroll; text-align: center">
                            <asp:DataList runat="server" ID="dtPaging" OnItemCommand="dtPaging_ItemCommand"
                                OnItemDataBound="dtPaging_ItemDataBound" RepeatDirection="Horizontal"
                                SeparatorStyle-Wrap="true" Style="height: auto">
                                <ItemTemplate>
                                    <asp:LinkButton runat="server" ID="lnkbtnPaging" Text="PageText" CommandArgument="PageIndex" Style="padding-right: 5px">                                    
                                    </asp:LinkButton>
                                </ItemTemplate>
                            </asp:DataList>
                        </div>    
<asp:HiddenField id="hdnScrollPos" runat="server"/>

            <script type="text/javascript">
                function BeginRequestHandler(sender, args)
                {
                    document.getElementById('<%=hdnScrollPos.ClientID %>').value = document.getElementById('<%=divDtPaging.ClientID %>').scrollLeft;
                }
                function EndRequestHandler(sender, args) {
                    document.getElementById('<%=divDtPaging.ClientID %>').scrollLeft = document.getElementById('<%=hdnScrollPos.ClientID %>').value;
                }

                if (window.Sys && Sys.WebForms && Sys.WebForms.PageRequestManager) {
                    var prm = Sys.WebForms.PageRequestManager.getInstance()
                    prm.add_beginRequest(BeginRequestHandler);
                    prm.add_endRequest(EndRequestHandler);
                }

        </script>

有什么理由不使用UpdatePanel吗?

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

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