473,396 Members | 2,036 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.

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

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
Nov 18 '05 #1
3 1486
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

Eeediot wrote:
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

--
mikeb
Nov 18 '05 #2
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]
"Eeediot" <ee*****@hotmail.com> wrote in message
news:6s********************@giganews.com...
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


Nov 18 '05 #3
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.

"Eeediot" <ee*****@hotmail.com> wrote in message
news:6s********************@giganews.com...
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

Nov 18 '05 #4

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

Similar topics

77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
7
by: PullnOutHair | last post by:
I know that I should know how to do this, but for some reason it is escapeing me right now and I was hoping someone here could help. I need to setup a pointer to the memory location 0x30000000. I...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
2
by: Lampa Dario | last post by:
Hi, where is this stupid error in this program? When I execute it, i receive a segmentation fault error. #include <stdio.h> int main(int argc, char *argv, char *env) { int i=0; int l=0; int...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
6
by: Alejandro Dubrovsky | last post by:
I see from googling around that this is a popular topic, but I haven't seen anyone saying "ah, yes, that works", so here it goes. How does one connect through a proxy which requires basic...
5
by: sherifffruitfly | last post by:
Hi all, I'm certain this is stupid, but I'd like to know how to keep a form open for, say, longer than 1ms. Here's the code I'm using to open my form: TestModelFileReaderForm modelInfo = new...
28
by: Joe Reynolds | last post by:
this is kinda off topic, but not totally. i have an ASP page that pulls text from a database, stores it in a variable, then uses <%= variable %to write the text inside an html table. the problem...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.