Stupid Dumba**ed question about ASP.NET...

E

Eeediot

I am trying to write code that will go through the existing list items in a
listbox and perform a string comparision with a supplied string. That
particular item will automatically selected if there is a match. I am
trying to use the IndexOf method to accomplish this task but, alas, I have
been unsuccessful.

If anyone has any ideas or suggestions it would be much appreciated.

Yes, I am a newbie to ASP.NET.
Yes, I am a newbie to VB.NET.

I am familiar with ASP.Classic and maybe that's my problem.
Here's my code:
=====
Protected lstIncdType as ListBox
...
Dim j as Integer
Dim Thingy as ListItem
...
j = 0

For Each Thingy In lstIncdType.Items
If Thingy.Text = CompareString Then
j = IndexOf(What!?!) 'Offending Code...
Exit For
End If

Next

lstIncdType.SelectedIndex = j
...
=====

TIA
 
M

mikeb

maybe something like:

Protected lstIncdType as ListBox
...
Dim j as Integer
Dim Thingy as ListItem
...

For j=0 to lstIncdType.Items.Count-1
Thingy = CType( lstIncdType.Items(j), ListItem)
If Thingy.Text = CompareString Then
lstIncdType.SelectedIndex = j
Exit For
End If
Next
 
K

Ken Cox [Microsoft MVP]

No need to feel bad about not knowing something. Nobody knows it all and we
all started as newbies.

Anyway, it seems like you're doing this the hard way. You can get the index
quite quickly like this:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim j As Integer
j = lstIncdType.Items.IndexOf _
(lstIncdType.Items.FindByText("Blue"))
Label1.Text = "Blue is item " & _
j.ToString & " (zero-based)"
CheckForText()
End Sub

If you need to check for a partial string, you can get it like this:


Sub CheckForText()
Dim j As Integer
Dim Thingy As ListItem
For Each Thingy In lstIncdType.Items
If InStr(Thingy.Text, "Blu") Then
j = lstIncdType.Items.IndexOf(Thingy)
Exit For
End If
Next
Label1.Text = "Search for 'Blu'... see zero-based item " & _
j.ToString
End Sub


<form id="Form1" method="post" runat="server">
<P>
<asp:ListBox id="lstIncdType" runat="server">
<asp:ListItem Value="Red">Red</asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
<asp:ListItem Value="Black">Black</asp:ListItem>
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
</asp:ListBox></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
</form>


You need to put in some error checking because the preceding will give false
results for the zero item.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
 
K

Kevin Spencer

Use a loop with a counter:

Dim i As Integer
For i = 0 To lstIncdType.Items.Count - 1
If lstIncdType.Items(i).Text = CompareString Then
lstIncdType.SelectedIndex = i
Exit For
End If
Next

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top