在 ASP.Net 页面上我有一个列表框。当其中的选定项目发生变化时,根据项目激活或停用下拉列表,并再次根据列表框项目选择下拉列表中的另一个项目。
我无法在 javascript 中执行列表框的 onSelectedItemChanged 操作,因为我需要为该操作查询 2 个不同的数据库。
我遇到的问题是,每当列表框中的选择更改事件在网站上出现部分回发时,所选列表框项目总是滚动到列表框的顶部(在 IE 中)或底部(在 chrome 中)。这对用户来说非常烦人。
这是用户选择项目之前列表框的样子 :
现在用户选择了一个项目,调用 selectionchanged 的事件处理程序,用数据填充下面的下拉列表,然后发生部分回发。对于此示例,用户单击列表框中的第 4 个“Klärschlamm”项。回发后,列表框如下所示:
突然,选中的项目位于列表框的顶部,而不是选中时的底部。
如何在回发后保持列表框的滚动位置?我尝试了很多我在网上找到的“解决方案”,将列表框和下拉列表包装在更新面板中,使用 javascript Begin- 和 EndRequestHandlers 等。我发现的解决方案都没有改变任何东西,唉。
这是我使用的 XAML:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="AbfallEdit.aspx.cs" %>
<asp:UpdatePanel runat="server" UpdateMode="Always" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:ListBox id="list" clientidmode="Static" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="updateKontamination" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:DropDownList ID="drop" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
SelectedIndexChanged 事件处理程序只做一些数据库查询,然后激活下拉列表并设置相应的索引。它对列表框本身没有任何作用。 如果它不是 PostBack,我只对列表框进行数据绑定(bind)。
最佳答案
这篇文章已经很老了,但以防其他人遇到同样的问题......
将标记更改为
<asp:UpdatePanel id="upList" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="otherControlToUpdateList" EventName="<EventName>" />
</Triggers>
<ContentTemplate>
<asp:ListBox id="list" clientidmode="Static" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="updateKontamination" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel id="upDrop" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="list" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:DropDownList ID="drop" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
这应该可以防止“upList”在选择更改时触发和重建“列表”。
关于c# - ASP.Net 列表框 : maintain listbox scrolling position after partial postback (not site scrolling position! ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17724751/