Mark
Mark

Reputation: 2760

How to compare a text with ITEMS inside a list box in c#

 string EmailDomain = useremail.Split('@')[1].Trim();
                foreach (ListItem li in lst_DomainList.Items)
                {
                    if (EmailDomain.Equals(li))
                    {
                    }
                 }

If the input is [email protected] then after split the EmailDomain value is email.com the value inside the list are

email
email.com

Here the second items in the list matches with the EmailDomain value,. but the way I have done it it says they dont match, what should I do

Upvotes: 2

Views: 1037

Answers (3)

Mike Christensen
Mike Christensen

Reputation: 91628

Shouldn't it be:

if (EmailDomain.Equals(li.Text))

Upvotes: 0

Jodaka
Jodaka

Reputation: 1247

ListItem li refers to the actual item in the list box. You should compare against li.Text

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500795

You're comparing the string with the ListItem itself. You probably want to compare with li.Value or li.Text...

string domain = userEmail.Split('@')[1].Trim();
foreach (ListItem li in lst_DomainList.Items)
{
    if (li.Value == domain)
    {
        ...
    }
 }

Upvotes: 4

Related Questions