Trouble setting the selectedindex of a listbox

  • Thread starter Thread starter John Kotuby
  • Start date Start date
J

John Kotuby

Hi all,

I think this should be a very easy thing to do, but I am having difficulty
finding the documentation or maybe just my implementaion is wrong. I fill a
single-select listbox with US State names and Abbreviations.
Here is the code:

If StateTable.Rows.Count > 0 Then
Dim lstValue, lstText As String
lststate.Items.Clear()
For Each row As DataRow In StateTable.Rows
lstValue = row.Item("id").ToString
lstText = row.Item("name").ToString
Me.lststate.Items.Add(New ListItem(lstText, lstValue))
Next
End If
Dim i As Integer
i = lststate.Items.IndexOf(lststate.Items.FindByValue(state))

'Response.Write(CStr(i))
'Response.End()

Me.lststate.SelectedIndex = i

The contents of the variable "state" which I am using for the search is
"CT", a value that definitely exists in the StateTable.
The displayed text is the "name" field from the StateTable. I am assuming
that the value is the "id" because of the way I am populating the listbox.

Me.lststate.Items.Add(New ListItem(lstText, lstValue))

The listbox is definitely populating but the displayed text at the top is
Alaska (the first state in the table).

I am trying to set the Selected Index according to a known state
abbreviation which exists in the listbox. It is correct in the table.
But, my debugging code that displays the value i returned by
lststate.Items.IndexOf(lststate.Items.FindByValue(state))
is always -1.

There must be an easy way to do this, but for the moment I am stumped.
Thanks for any help...
 
Hi all,

I think this should be a very easy thing to do, but I am having difficulty
finding the documentation or maybe just my implementaion is wrong. I fill a
single-select listbox with US State names and Abbreviations.
Here is the code:

If StateTable.Rows.Count > 0 Then
Dim lstValue, lstText As String
lststate.Items.Clear()
For Each row As DataRow In StateTable.Rows
lstValue = row.Item("id").ToString
lstText = row.Item("name").ToString
Me.lststate.Items.Add(New ListItem(lstText, lstValue))
Next
End If
Dim i As Integer
i = lststate.Items.IndexOf(lststate.Items.FindByValue(state))

'Response.Write(CStr(i))
'Response.End()

Me.lststate.SelectedIndex = i

The contents of the variable "state" which I am using for the search is
"CT", a value that definitely exists in the StateTable.
The displayed text is the "name" field from the StateTable. I am assuming
that the value is the "id" because of the way I am populating the listbox.

Me.lststate.Items.Add(New ListItem(lstText, lstValue))

The listbox is definitely populating but the displayed text at the top is
Alaska (the first state in the table).

I am trying to set the Selected Index according to a known state
abbreviation which exists in the listbox. It is correct in the table.
But, my debugging code that displays the value i returned by
lststate.Items.IndexOf(lststate.Items.FindByValue(state))
is always -1.

There must be an easy way to do this, but for the moment I am stumped.
Thanks for any help...

Are you sure CT is the value is a good question. View your HTML output
(the <options>) to make sure nothing unusual is happening - for
instance, perhaps there are spaces accidentally appended and you need to
trim them? So, perhaps CT is not one of the values? Maybe "CT "
instead. The other thing I'd look for is a missing cast.

Set a break point and debug the code.

PS: Add another condition here
If StateTable.Rows.Count > 0 Then
to check that StateTable is not null. In general, always check that
DataSets and DataTables are not null before you use them.
 
Thanks for the responses guys and also the suggestion to check for
StateTable is NOT NULL.
I presume that by your answers my implementation is fine, since you are both
concentrating on the values.

I will look more closely at all the values...maybe iterate throug both the
table and lisbox contents to make sure there is a match in every case, as
well as checking the value of the "state" variable.

It would be nice to be able to just "set" the SelectedValue. I am wondering
why that more direct route is not available.
 
It would be nice to be able to just "set" the SelectedValue. I am
wondering why that more direct route is not available.

lstState.SelectedValue = "CT"

or

lstState.Items.FindByValue("CT").Selected = True
 
Thanks Mark,
I must have misread MSDN doc on that. I thought that only the SelectedIndex
was settable.

Yep there was a dumb mistake on my part. The State abbreviations were coming
from SQL lookup table in a field defined as Char(10) and I wasn't trimming
them. Some days I wonder where my head is.
 
I must have misread MSDN doc on that. I thought that only the
SelectedIndex was settable.

Easily done...
Yep there was a dumb mistake on my part. The State abbreviations were
coming from SQL lookup table in a field defined as Char(10) and I wasn't
trimming them. Some days I wonder where my head is.

Are US state abbreviations ever anything other than two characters in
length...?
 
Back
Top