List box giving me error

T

tshad

I am getting an error on my Dropdownlist box if there is nothing in it when
I double click on the item I want to look at. I have the ddl setup to allow
double clicks on the items to look at like so:

Me.StoredSearches.Attributes("ondblClick") =
"__doPostBack('LbxSender','')"

If Request.Form("__EVENTTARGET") = "LbxSender" Then
Dim strSelected As String
strSelected = StoredSearches.SelectedItem.Value.ToString
Call SelectSearch(strSelected)
trace.warn("storedSearches.SelectedItem is nothing")
End If

If there is nothing in the box and you double click anywhere, you get the
following error:

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

If there is at least one item in the ddl and you haven't already selected
anything, you will get the next index after the last item. For example, if
there is 1 item in the ddl (which would be index 0), you will get 1.

If there is nothing, in the box - you get the error:

I can fix it by doing this:
If Request.Form("__EVENTTARGET") = "LbxSender" Then
Dim strSelected As String
if not storedSearches.SelectedItem is nothing then
strSelected = StoredSearches.SelectedItem.Value.ToString
Call SelectSearch(strSelected)
else
trace.warn("storedSearches.SelectedItem is nothing")
end if
End If

All I do is check if "storedSearches.SelectedItem" (my dropdownlist) is
nothing.

I would have thought that I would get a -1 as an index.

If there is nothing in the ddl and you double click and you get null
(nothing) for the selectedItem, why wouldn't you get the same for selecting
an item past the list?

Thanks,

Tom
 
T

tshad

Never mind.

Had a little brain fade.

I should have been looking at SelectedIndex (which will be set to -1) and
not SelectedItem.

Tom
 
T

tshad

Actually,

I did find that if there is something in the ddl and you select below the
last item (and nothing is selected at the time), you will get a 0 as
SelectedIndex (instead of -1) and whatever ..items.count is.

Why is this?

This would mean that the only way to test if there was a valid choice is to
check first if item.count is = 0 (to see if there is anything in the list)
and then to see if the SelectedItem = ..items.count (not a valid choice).
SelectedIndex doesn't help as it would be 0 (which is valid - just not what
was chosen).

Tom
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,153
Latest member
NamKaufman
Top