473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Locking a Listbox position

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?
Jul 21 '05 #1
9 4009
On Tue, 29 Jun 2004 01:58:01 -0700, "Irene"
<Ir***@discussions.microsoft.com> wrote:
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.

Jul 21 '05 #2
.... or don't repopulate the DropDownList on each post-back and enable
viewstate for that control.

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

Dan Brussee wrote:
On Tue, 29 Jun 2004 01:58:01 -0700, "Irene"
<Ir***@discussions.microsoft.com> wrote:

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.

Jul 21 '05 #3
This is a better solution. If you are populating the listbox
independent of IsPostBack, that is a "bad thing" :)
On Tue, 29 Jun 2004 21:30:59 -0400, Sean Bright <se**@noreply.com>
wrote:
... or don't repopulate the DropDownList on each post-back and enable
viewstate for that control.

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

Dan Brussee wrote:
On Tue, 29 Jun 2004 01:58:01 -0700, "Irene"
<Ir***@discussions.microsoft.com> wrote:

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.


Jul 21 '05 #4
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?
"Dan Brussee" wrote:
This is a better solution. If you are populating the listbox
independent of IsPostBack, that is a "bad thing" :)
On Tue, 29 Jun 2004 21:30:59 -0400, Sean Bright <se**@noreply.com>
wrote:
... or don't repopulate the DropDownList on each post-back and enable
viewstate for that control.

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

Dan Brussee wrote:
On Tue, 29 Jun 2004 01:58:01 -0700, "Irene"
<Ir***@discussions.microsoft.com> wrote:
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.


Jul 21 '05 #5
Ah. That is different. Personally, If there is only one possible
selection, I would opt for a dropdown list.

On Tue, 29 Jun 2004 21:15:01 -0700, "Irene"
<Ir***@discussions.microsoft.com> wrote:
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?
"Dan Brussee" wrote:
This is a better solution. If you are populating the listbox
independent of IsPostBack, that is a "bad thing" :)
On Tue, 29 Jun 2004 21:30:59 -0400, Sean Bright <se**@noreply.com>
wrote:
>... or don't repopulate the DropDownList on each post-back and enable
>viewstate for that control.
>
>if (!this.IsPostBack)
>{
> // Populate your listbox here
>}
>
>Dan Brussee wrote:
>
>> On Tue, 29 Jun 2004 01:58:01 -0700, "Irene"
>> <Ir***@discussions.microsoft.com> wrote:
>>
>>
>>>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.
>>



Jul 21 '05 #6
> 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
Jul 21 '05 #7
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…?


"Jay Vinton" wrote:
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

Jul 21 '05 #8
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.
"Jay Vinton" wrote:
Hi Irene,

This works on a WinForm but I haven't tested on web.

With ListBox1

For i = 0 To .Items.Count - 1

If (.Items(i).DisplayText = strItemToSelect) Then

.SelectedIndex = i
Exit For

End If
Next
End With

Hope that helps,

Jay

Jul 21 '05 #9
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

Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Bass | last post by:
I'm looking to develop a listbox with in-place editing where as each item is selected, it grows to fit in all the text boxes. When the item is deselected, it shrinks back to its original size. The...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
8
by: Steve Schroeder | last post by:
For some reason I cannot get the OnSelectedIndexChanged event to fire for a listbox I have on a page. I'm able to populate the listbox with data from a stored procedure, but cannot trigger the...
9
by: Irene | last post by:
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...
3
by: superjacent | last post by:
Hope someone can help. I have a listbox displaying time periods in blocks of 15 mins for a 24 hour period, all up 96 rows. The listbox can only visibly show 20 rows a time. The default...
2
by: teo | last post by:
I have a Listbox, if I set EnableViewStarte = False the AutopostaBack fired by SelectedIndexChanged doesn't work. The 'SelectedIndexChanged' event should call
7
by: Lit | last post by:
Hi, How can I capture the vertical scroll bar position for a Listbox. I have a Listbox of 100 items + when I click on it I post back remove the item selected. After returning to the client...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.