Find if value exists in drop down

  • Thread starter Thread starter MattB
  • Start date Start date
M

MattB

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
 
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]
 
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
 
Back
Top