列表框在自动回发时滚动到顶部

发布于 2024-12-02 10:01:16 字数 60 浏览 3 评论 0原文

为什么当自动回发打开时,asp.net 列表框在选择某个项目时总是滚动到顶部?我怎样才能防止这种情况发生?

Why does asp.net listbox always scroll to top upon selecting an item when autopostback is on? How can I prevent this from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

菩提树下叶撕阳。 2024-12-09 10:01:17

你有几个选择。您可以在页面指令中将MaintainScrollPositionOnPostBack 设置为true,也可以将列表框放在更新面板中并使用AJAX 来维护滚动位置。

页面指​​令选项:

<pages maintainScrollPositionOnPostBack="true" />

更新面板选项:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:ListBox ID="ListBox1" runat="server" ...>
    </ContentTemplate>
</asp:UpdatePanel>

You have a couple of options. You can either set MaintainScrollPositionOnPostBack to true in the page directive, or you can put the listbox in an update panel and use AJAX to maintain scroll position.

Page directive option:

<pages maintainScrollPositionOnPostBack="true" />

Update panel option:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:ListBox ID="ListBox1" runat="server" ...>
    </ContentTemplate>
</asp:UpdatePanel>
塔塔猫 2024-12-09 10:01:16

我在 javascript 中添加了以下 jquery 来解决该问题。我不记得在哪里找到了解决方案,但它就在这里。只需添加目标控件的位置 - $get('YourDiv_YourPanel')。

<script type="text/javascript">
    //Maintain scroll position in given element or control
    var xInputPanel, yInputPanel;
    var xProductPanel, yProductPanel;
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(BeginRequestHandler);
    prm.add_endRequest(EndRequestHandler);
    function BeginRequestHandler(sender, args) {
        yInputPanel = $get('MainContent_InputPanel').scrollTop;
        yProductPanel = $get('MainContent_ProductPanel').scrollTop;
    }
    function EndRequestHandler(sender, args) {
        $get('MainContent_InputPanel').scrollTop = yInputPanel;
        $get('MainContent_ProductPanel').scrollTop = yProductPanel;
    }
</script>

I added the following jquery in javascript to fix the issue. I cannot remember where I found the solution but here it is. Just add the location of your target control - $get('YourDiv_YourPanel').

<script type="text/javascript">
    //Maintain scroll position in given element or control
    var xInputPanel, yInputPanel;
    var xProductPanel, yProductPanel;
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(BeginRequestHandler);
    prm.add_endRequest(EndRequestHandler);
    function BeginRequestHandler(sender, args) {
        yInputPanel = $get('MainContent_InputPanel').scrollTop;
        yProductPanel = $get('MainContent_ProductPanel').scrollTop;
    }
    function EndRequestHandler(sender, args) {
        $get('MainContent_InputPanel').scrollTop = yInputPanel;
        $get('MainContent_ProductPanel').scrollTop = yProductPanel;
    }
</script>
画▽骨i 2024-12-09 10:01:16

我遇到了同样的问题,我找到了一种使用更新面板来解决此问题的方法。我相信这是由于列表框在回发时更新,因此更新面板可以帮助我们确保它不会更新。确保列表框位于条件更新面板内,并将子项作为触发器设置为“false”:

 <asp:UpdatePanel runat="server" ID="updtpnlSearchResults" UpdateMode="Conditional" ChildrenAsTriggers="false">

现在,将列表框选择更改时需要更改的内容放入其自己的更新面板内。这样列表框就不会刷新。

I had the same issue, and I figured out a way to do it with update panels. I believe it's due to the listbox being updated on postback, so update panels can help us make sure it doesn't update. Make sure the listbox is inside a conditional update panel with children as triggers set to 'false':

 <asp:UpdatePanel runat="server" ID="updtpnlSearchResults" UpdateMode="Conditional" ChildrenAsTriggers="false">

Now put whatever needs to be changed when the list box selection changes inside an update panel of its own. That way the listbox doesn't get refreshed.

百善笑为先 2024-12-09 10:01:16

您需要此页面指令:

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

You need this page directive:

<%@ Page MaintainScrollPositionOnPostback="true" ... %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文