简体   繁体   中英

ASP.Net Listbox: maintain listbox scrolling position after partial postback (not site scrolling position!)

On an ASP.Net page I have a listbox. When the selected items in it changes, a dropdownlist gets activated or deactivated depending on the item, and again depending on the listbox item another item in the dropdownlist is selected.

I can't do the onSelectedItemChanged action of the listbox in javascript because I need to query 2 different databases for the action.

The problem I have is that whenever there is a partial postback on the site from the selection changed event in the listbox, the selected listbox item always scrolls to top (in IE) or bottom (in chrome) of the listbox. This is extremely irritating for the user.

Here's how the listbox looks before the user selects an item 在此输入图像描述 :

Now the user selects an item, the eventhandler for selectionchanged gets called, fills the dropdownlist below with data, and a partial postback occurs. For this example the user clicks the "Klärschlamm" item in the listbox, the 4th one. After the postback this is what the listbox looks like:

在此输入图像描述

Suddenly the selected item is on top of the listbox instead of bottom where it was when it was selected.

What can I do to maintain the scrolling position of the listbox after the postback? I tried a lot of "solutions" I've found on the net, wrapping the listbox and dropdown in updatepanels, using javascript Begin- and EndRequestHandlers, etc. None of the solutions I've found changed anything, alas.

Here's the XAML I use:

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

The SelectedIndexChanged event handler does nothing but a some database queries and then activating the dropdownlist and setting the respective index. It doesn't do anything with the listbox itself. And I only databind the listbox if it's not a PostBack.

this post is pretty old but in case someone else is having the same issue...

change the markup to

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

This should keep "upList" from firing and rebuilding the "list" on selection change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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