Locking a Listbox position

G

Guest

I'm developing a Web application where a user selects from a listbox which can have many items. The initial display only shows about 10 items.

After a postback, the listbox is automatically scrolled back to the top, so the selected item is mostly no longer visible. How can I keep the position, so that the selected item is still shown?
 
D

Dan Brussee

I'm developing a Web application where a user selects from a listbox which can have many items. The initial display only shows about 10 items.

After a postback, the listbox is automatically scrolled back to the top, so the selected item is mostly no longer visible. How can I keep the position, so that the selected item is still shown?


Set the listbox's selectedindex property on postback.
 
S

Sean Bright

.... or don't repopulate the DropDownList on each post-back and enable
viewstate for that control.

if (!this.IsPostBack)
{
// Populate your listbox here
}
 
D

Dan Brussee

This is a better solution. If you are populating the listbox
independent of IsPostBack, that is a "bad thing" :)
 
G

Guest

Sorry, I didn’t express myself precisely enough:

The size of the listbox is such that it shows only about 10 items, e.g. if you want to see more, you have to scroll down. The listbox is only populated once, and in the way Sean suggested: if (! IsPostBack). The values never change. View State is enabled.

On postback, I do not loose the SelectedIndex – the previously selected item is still highlighted. But because the listbox auto-scrolls back to the top, it is no longer immediately visible.

How can I lock the scroll position?
 
D

Dan Brussee

Ah. That is different. Personally, If there is only one possible
selection, I would opt for a dropdown list.
 
G

Guest

After a postback, the listbox is automatically scrolled back to the top, so the selected item is mostly no longer visible. How can I keep the position, so that the selected item is still shown?

Hi Rebecca,

You can work with the listbox's SelectedIndex and TopIndex properties to make sure your item is visible. There is some sample code in the help under TopIndex that may be useful.

Jay
 
G

Guest

The TopIndex property seems exactly like what I’m after – unfortunately it’s only supported for listboxes on Windows.Forms, but I’m developing a Web application…

And in answer to Dan: I do need multiple selects, therefore the drop-down list is not an option, either.

Does that mean it can’t be done…?
 
G

Guest

Hi Jay,

I'm not loosing the selected index - that one I still have after a postback - I'm loosing the scrolling position. I tried your piece of code anyway, but as expected, it doesn't help.
 
G

Guest

Well, darn.

I don't do any web dev so I can't test the idea, but I'm surprised that selecting an item in code doesn't bring it into the viewable area of the listbox.

One would think that a ListBox would have an EnsureVisible property like the treeview in VB6. As much as I enjoy fooling around with these seemingly small usability issues, it really wastes a lot of time.

Or maybe it's just a bug. I'm using the May release of Whidbey and am downloading beta 1 now. Maybe it will behave differently.

Please let us know if you find a solution.

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top