Re: Find if value exists in drop down

  • Thread starter Ken Cox [Microsoft MVP]
  • Start date
K

Ken Cox [Microsoft MVP]

Hi Matt,

One way is to use the FindByText method of the ddl. It looks through the
items and tries to find a match (case-sensitive).

If the item is Nothing, there's no match. If it returns an object, you've
found what you were after. Here's some code that shows what I mean:


Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim lstItem As ListItem
lstItem = DropDownList1.Items.FindByText _
(TextBox1.Text)
If IsNothing(lstItem) Then
Label1.Text = "Not Found"
Else
Label1.Text = "Found"
End If
End Sub

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="Red">Red</asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
</asp:DropDownList></P>
<P>
<asp:TextBox id="TextBox1" runat="server">Yellow</asp:TextBox></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
<P>
<asp:Button id="Button1" runat="server" Text="Find"></asp:Button></P>
</form>

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
M

MattB

Thanks! I think so, but before I could implement it the client changed their
minds about that feature so it no longer applies. I've filed it for future
reference and will give it a show when (if?) I have time.

Matt
Hi Matt,

One way is to use the FindByText method of the ddl. It looks through
the items and tries to find a match (case-sensitive).

If the item is Nothing, there's no match. If it returns an object,
you've found what you were after. Here's some code that shows what I
mean:


Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim lstItem As ListItem
lstItem = DropDownList1.Items.FindByText _
(TextBox1.Text)
If IsNothing(lstItem) Then
Label1.Text = "Not Found"
Else
Label1.Text = "Found"
End If
End Sub

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="Red">Red</asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
</asp:DropDownList></P>
<P>
<asp:TextBox id="TextBox1" runat="server">Yellow</asp:TextBox></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
<P>
<asp:Button id="Button1" runat="server"
Text="Find"></asp:Button></P> </form>

Does this help?

Ken
Microsoft MVP [ASP.NET]


MattB said:
I have some drop-down lists I'd like to pre-populate from values
in a database. I've found that if I just blindly assign the
SelectedValue, I run
into problems if what I'm trying to assign isn't a choice for that
dropdown.
So what I need to know, is how do I test for the existence of a
value in
a dropdown list? Seems like it should be easy, but I haven't been
able to make it work yet.

Thanks!

Matt
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top