View Full Version : Compare item in dropdownlist with string
calvin
07-15-2004, 06:23 AM
I want to validate all items in dropdownlist with string/label.
So, I try to write like this:
If dropdownlist.Items.<u>Contains(label.text)</u> Then
.
.
.
But I get an error of "Value of String cannot be converted to System.Web.UI.WebControls.ListItem"
Does anybody know how to convert it??
Thank you for any help.
Calvin
calvin
07-15-2004, 07:34 AM
In fact I want to know does the dropdownlist contain the value same as label.text
For example If the label.text = "Hello world", check the dropdownlist contain the item "Hello world" or not.
Calvin
pjoyce
07-16-2004, 02:20 AM
.FindByText is, I think, what you're looking for. This function returns null (Nothing) if it doesn't find an item.
I'll try this in VB, but don't yell at me if the syntax is wrong. I'm not sure if != is "not Equal" in VB...:)
[quote]
Ifdropdownlist.Items.FindByText(label.Text)!=Nothi ngThen
...
...
...
</CODE>
There is also a FindByValue which will find something based on the value of the ListItem.
[b]quote:Originally posted by calvin
I want to validate all items in dropdownlist with string/label.
So, I try to write like this:
If dropdownlist.Items.<u>Contains(label.text)</u> Then
.
.
.
But I get an error of "Value of String cannot be converted to System.Web.UI.WebControls.ListItem"
Does anybody know how to convert it??
Thank you for any help.
Calvin
</blockquote id="quote"></font id="quote">[quote]</CODE>[quote]</CODE>
bruce
07-16-2004, 12:19 PM
Contains method is expecting System.Web.UI.WebControls.ListItem as the input parameter
In order to get the item name, you'll have to look at
DropDownList.Items(0).ToString
You may need to loop through it.
[b]quote:Originally posted by calvin
I want to validate all items in dropdownlist with string/label.
So, I try to write like this:
If dropdownlist.Items.<u>Contains(label.text)</u> Then
.
.
.
But I get an error of "Value of String cannot be converted to System.Web.UI.WebControls.ListItem"
Does anybody know how to convert it??
Thank you for any help.
Calvin
</blockquote id="quote"></font id="quote">
vBulletin® ©Jelsoft Enterprises Ltd.