Matching text in the drop down...

  • Thread starter Robert Strickland
  • Start date
R

Robert Strickland

I load up a list of countries into a asp:dropdownlist web control. I need to
match the text to pre-select the country for exiting customer. Problem is
the matching text can be 'France' or 'FRANCE' where I match on my drop down
for 'France' but not 'FRANCE'. Is there anyway to match 'FRANCE'?
 
J

Jeffrey Tan[MSFT]

Hi Robert,

Thank you for posting in the community!

Based on my understanding, you meet the text matching problem in
DropDownList.
That is: The Country text in your dropdownlist is "France", while the
maching text from the customer may be "France" or "FRANCE". Then the
"France" matches well, but "FRANCE" does not match.

======================================================
I think the behavior depends on your text matching implementation. How do
you implement your program logic?

I suppose you loop through all the items in the DropDownList and use
ListItem.Text property to match.

So I wrote a sample project like this:

1). The items in the DropDownList are:
USA
France
England
Canada

2). Match code:
private void Button1_Click(object sender, System.EventArgs e)
{
string matchingstr="France";
for(int i=0; i<DropDownList1.Items.Count;i++)
{
if(DropDownList1.Items.Text.Equals(matchingstr))
{
DropDownList1.SelectedIndex=i;
}
}
}

But using matchingstr="FRANCE" will fail.

So I think you can use String class to make them the same lower-case or
upper-case:
private void Button1_Click(object sender, System.EventArgs e)
{
string matchingstr="FRANCE";
for(int i=0; i<DropDownList1.Items.Count;i++)
{
if(DropDownList1.Items.Text.ToLower().Equals(matchingstr.ToLower()))
{
DropDownList1.SelectedIndex=i;
}
}
}

This will work.
If I misunderstand you, please feel free to tell me.

==========================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
R

Robert Strickland

Thanks for the code. This will work but I have a question concerning the
FindByText method from the asp:dropdownlist web control. It appears the
search is case sensitive. Is there anyway around this?

Bob
 
W

Wardeaux

Robert,
there may be a "slicker" way to get this done, but I've found I have to
convert BOTH strings to either Upper or Lower case in order to get a case
insensative compare. Options:
1) Convert your DropDownList text to upper case and then
MySel.SelectedIndex =
MySel.Items.IndexOf(MySel.Items.FindByText(strToFind.ToUpper()))
2) If you desire the mixed case DropDownList text (looks MUCH nicer) then
you can set the VALUE of each item to Upper case when loading the
DropDownList and then
MySel.SelectedIndex =
MySel.Items.IndexOf(MySel.Items.FindByValue(strToFind.ToUpper()))

The only other option I see is to do like Jeffery suggested and run the list
item-by-item. This may be necessary if the Value is an ID from a
"Countries" table and you still want mixed case in your DropDownList...

Hope this helps!
wardeaux

Robert Strickland said:
Thanks for the code. This will work but I have a question concerning the
FindByText method from the asp:dropdownlist web control. It appears the
search is case sensitive. Is there anyway around this?

Bob

"Jeffrey Tan[MSFT]" said:
Hi Robert,

Thank you for posting in the community!

Based on my understanding, you meet the text matching problem in
DropDownList.
That is: The Country text in your dropdownlist is "France", while the
maching text from the customer may be "France" or "FRANCE". Then the
"France" matches well, but "FRANCE" does not match.

======================================================
I think the behavior depends on your text matching implementation. How do
you implement your program logic?

I suppose you loop through all the items in the DropDownList and use
ListItem.Text property to match.

So I wrote a sample project like this:

1). The items in the DropDownList are:
USA
France
England
Canada

2). Match code:
private void Button1_Click(object sender, System.EventArgs e)
{
string matchingstr="France";
for(int i=0; i<DropDownList1.Items.Count;i++)
{
if(DropDownList1.Items.Text.Equals(matchingstr))
{
DropDownList1.SelectedIndex=i;
}
}
}

But using matchingstr="FRANCE" will fail.

So I think you can use String class to make them the same lower-case or
upper-case:
private void Button1_Click(object sender, System.EventArgs e)
{
string matchingstr="FRANCE";
for(int i=0; i<DropDownList1.Items.Count;i++)
{
if(DropDownList1.Items.Text.ToLower().Equals(matchingstr.ToLower()))
{
DropDownList1.SelectedIndex=i;
}
}
}

This will work.
If I misunderstand you, please feel free to tell me.

==========================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

 
R

Robert Strickland

Thanks

Wardeaux said:
Robert,
there may be a "slicker" way to get this done, but I've found I have to
convert BOTH strings to either Upper or Lower case in order to get a case
insensative compare. Options:
1) Convert your DropDownList text to upper case and then
MySel.SelectedIndex =
MySel.Items.IndexOf(MySel.Items.FindByText(strToFind.ToUpper()))
2) If you desire the mixed case DropDownList text (looks MUCH nicer) then
you can set the VALUE of each item to Upper case when loading the
DropDownList and then
MySel.SelectedIndex =
MySel.Items.IndexOf(MySel.Items.FindByValue(strToFind.ToUpper()))

The only other option I see is to do like Jeffery suggested and run the list
item-by-item. This may be necessary if the Value is an ID from a
"Countries" table and you still want mixed case in your DropDownList...

Hope this helps!
wardeaux

Robert Strickland said:
Thanks for the code. This will work but I have a question concerning the
FindByText method from the asp:dropdownlist web control. It appears the
search is case sensitive. Is there anyway around this?

Bob

"Jeffrey Tan[MSFT]" said:
Hi Robert,

Thank you for posting in the community!

Based on my understanding, you meet the text matching problem in
DropDownList.
That is: The Country text in your dropdownlist is "France", while the
maching text from the customer may be "France" or "FRANCE". Then the
"France" matches well, but "FRANCE" does not match.

======================================================
I think the behavior depends on your text matching implementation. How do
you implement your program logic?

I suppose you loop through all the items in the DropDownList and use
ListItem.Text property to match.

So I wrote a sample project like this:

1). The items in the DropDownList are:
USA
France
England
Canada

2). Match code:
private void Button1_Click(object sender, System.EventArgs e)
{
string matchingstr="France";
for(int i=0; i<DropDownList1.Items.Count;i++)
{
if(DropDownList1.Items.Text.Equals(matchingstr))
{
DropDownList1.SelectedIndex=i;
}
}
}

But using matchingstr="FRANCE" will fail.

So I think you can use String class to make them the same lower-case or
upper-case:
private void Button1_Click(object sender, System.EventArgs e)
{
string matchingstr="FRANCE";
for(int i=0; i<DropDownList1.Items.Count;i++)
{
if(DropDownList1.Items.Text.ToLower().Equals(matchingstr.ToLower()))
{
DropDownList1.SelectedIndex=i;
}
}
}

This will work.
If I misunderstand you, please feel free to tell me.

==========================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by
to
 
J

Jeffrey Tan[MSFT]

Hi Robert,

Thanks very much for your feedback.

I am glad you got what you want.
Actually, I think you problem's key point is the case-sensitive problem.
Which should use the String.ToUpper() or String.ToLower method to
workaround.

Anyway, you got your need. :)

If you have further concern, please feel free to tell me, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Forum statistics

Threads
474,265
Messages
2,571,071
Members
48,771
Latest member
ElysaD

Latest Threads

Top