【问题标题】:ASP.NET Maintain scroll position after postback in div inside data list and user controlASP.NET在数据列表和用户控件内的div中回发后保持滚动位置
【发布时间】:2011-10-30 01:58:35
【问题描述】:

我的情况如下所示:我有 x 轴可滚动 div,其中包含显示页数的按钮。此 div 位于负责在我的网站上显示新闻的用户控件内的数据列表中。

这是这个 div 和 datalist 的代码以及页码。

<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>

回发后如何保持该 div 的 x 轴位置?我尝试了几种技巧,javascript,但我无法弄清楚。

【问题讨论】:

  • 您找到解决方案了吗?我正在寻找类似的解决方案。如果你有,请告诉我。
  • 我搞定了。如果您现在需要它,请找到我的答案..我知道它为时已晚,但我最近遇到了它并找到了解决方案。

标签: c# asp.net user-controls datalist maintainscrollpositionon


【解决方案1】:

有什么理由不使用 UpdatePanel?

【讨论】:

  • 我无法让它工作。我正在尝试添加触发器,但找不到页面按钮控件:/它说“在 UpdatePanel 'UpdatePanel2' 中找不到触发器的 ID 为 'ContentPlaceHolder1_News1_dlPaging_lnkbtnPaging_0' 的控件。”但是当我查找生成的 HTML 标记时,控件 ID 很好。我不知道该怎么办。是我还是 asp.net 中的用户控件很烂?
  • 我们能看到更新面板的标签吗?
【解决方案2】:
<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-08
    • 2013-07-17
    • 2011-10-18
    • 2019-08-03
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多