0
votes

Refering to a C#, .net, System.Web.UI.WebControls.Lisbox which has a multiple select option true and needs to do a postback every time selected index changes.

Problem is, the [SelectedIndex / SelectedItem / SelectedValue] is always on the value of the 1st selected item. Clicking on the second item, third item, does not change the SelectedIndex, causing the listbox to reload and scroll to the highest selected item position.

5
The closest solution I've got is a global javascript var to track the last clicked item. But that is not ideal, on prerender, the listbox jumps to the top most selected item (before any javascript can kick in).bcm
Also...when I debug the SelectedIndexChange function, there is a list of selected items cached, but no indication of which is the last one added.bcm
I guess there is no real way of doing this in back-end while still using the postback is true option for Listbox. Closing this thread if that is so.bcm

5 Answers

0
votes

Brandon - can you share the code where you are loading the listbox. Below are few probable reasons for ListBox not to hold its selection:

  1. Reloading list box in post back (after post data has been processed)
  2. Dynamically loading list box late in page life cycle so that there is no chance to process post back data
  3. Dynamically loading list box (perhaps in templated parent) so that its Client ID changes (or specifically, ClientID is different when post data processing happens).
0
votes

The SelectedIndex will always point to the lowest selected index. The definition of the ListBox.SelectedIndex property:

Gets or sets the lowest ordinal index of the selected items in the list.

Manually setting the selectedindex property will de-select all other selections, and I am pretty sure that there is no way (in .net code) to scroll down to a particular selection.

I don't have a clear picture of what you're trying to do, but perhaps you can capture all selected indexes by using the GetSelectedIndices ListBox function, and compare against the previously captured selected indexes to determine the most recent selected item.

0
votes

Brandon Make sure that you don't populate the ListBox every time the page loads

0
votes

There is no way to do this as far this control is concerned.

0
votes

This is strictly a scrolling problem.

To explicitly control the scroll value of a ListBox (or any element for that matter), you need to use JavaScript to set the scrollTop property:

document.all.getElementById("elementID").scrollTop = VALUE.

So alter the JavaScript handler that does the post-back for your list box, which looks like "javascript:__doPostBack(...". Replace that with a call to a custom function that grabs the current scrollTop value, saves it to a hidden variable, then calls __doPostBack as it was originally supposed to.

After the round trip, that hidden value should persist back to the client, so in a JavaScript page load event, simply read that hidden value and reapply it to the scrollTop of your list box. Your list box will then be scrolled exactly where it was before the post back occurred.