listbox

S

susie

I have an web page with four listboxes created in asp.net. Each listbox has more than 100 items. Each time I select one item added to another list, the listbox that I choose items from refreshed to the top. I have to scroll down the list again each time I chose an item.

How to leave the listbox stay where I left from last selection

Thank you.
 
F

Felbrigg

Do you mean that the first list box you choose from is "forgetting" the item
you selected?

susie said:
I have an web page with four listboxes created in asp.net. Each listbox
has more than 100 items. Each time I select one item added to another list,
the listbox that I choose items from refreshed to the top. I have to scroll
down the list again each time I chose an item.
 
K

Ken Cox [Microsoft MVP]

That shouldn't happen. It should keep track of where it was.

Is it possible that you are binding your listbox on each page load? You
should check for Not IsPostBack to prevent that. In this example, the
selected colour name reappears:


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim strSelectedColor As String
Dim li As New ListItem
Dim enumColor As New KnownColor
Dim Colors As Array = _
[Enum].GetValues(enumColor.GetType())
DropDownList1.DataSource = Colors
DropDownList1.DataBind()
strSelectedColor = DropDownList1.Items(0).Text
li.Value = 0
li.Text = "--ALL"
DropDownList1.Items.Insert(0, li)
End If
End Sub 'Page_Load
 
K

Ken Cox [Microsoft MVP]

Oops. You wanted a Listbox:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim strSelectedColor As String
Dim li As New ListItem
Dim enumColor As New KnownColor
Dim Colors As Array = _
[Enum].GetValues(enumColor.GetType())
ListBox1.DataSource = Colors
ListBox1.DataBind()
strSelectedColor = ListBox1.Items(0).Text
li.Value = 0
li.Text = "--ALL"
ListBox1.Items.Insert(0, li)
End If
End Sub
Private Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Label1.Text = ListBox1.SelectedItem.Text
End Sub
 
F

Felbrigg

Thats where I was heading Ken, good pointer, good code sample.


Ken Cox said:
Oops. You wanted a Listbox:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim strSelectedColor As String
Dim li As New ListItem
Dim enumColor As New KnownColor
Dim Colors As Array = _
[Enum].GetValues(enumColor.GetType())
ListBox1.DataSource = Colors
ListBox1.DataBind()
strSelectedColor = ListBox1.Items(0).Text
li.Value = 0
li.Text = "--ALL"
ListBox1.Items.Insert(0, li)
End If
End Sub
Private Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Label1.Text = ListBox1.SelectedItem.Text
End Sub

susie said:
I have an web page with four listboxes created in asp.net. Each listbox
has more than 100 items. Each time I select one item added to another
list, the listbox that I choose items from refreshed to the top. I have to
scroll down the list again each time I chose an item.

How to leave the listbox stay where I left from last selection?

Thank you.
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top