[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.webcontrols

Matching text in the drop down...

Robert Strickland

2/5/2004 2:34:00 PM

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'?


5 Answers

v-jetan

2/6/2004 1:43:00 AM

0


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[i].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[i].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.

Robert Strickland

2/6/2004 1:39:00 PM

0

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]"" <v-jetan@online.microsoft.com> wrote in message
news:snmYTKF7DHA.1992@cpmsftngxa07.phx.gbl...
>
> 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[i].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[i].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.
>


Wardeaux

2/6/2004 4:15:00 PM

0

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" <strickra@rjsonline.net> wrote in message
news:%23FPFKaL7DHA.2460@TK2MSFTNGP09.phx.gbl...
> 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]"" <v-jetan@online.microsoft.com> wrote in message
> news:snmYTKF7DHA.1992@cpmsftngxa07.phx.gbl...
> >
> > 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[i].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[i].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.
> >
>
>


Robert Strickland

2/6/2004 6:23:00 PM

0

Thanks

"Wardeaux" <wardeaux@bellsouth.net> wrote in message
news:%237WwvxM7DHA.3860@tk2msftngp13.phx.gbl...
> 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" <strickra@rjsonline.net> wrote in message
> news:%23FPFKaL7DHA.2460@TK2MSFTNGP09.phx.gbl...
> > 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]"" <v-jetan@online.microsoft.com> wrote in message
> > news:snmYTKF7DHA.1992@cpmsftngxa07.phx.gbl...
> > >
> > > 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[i].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[i].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.
> > >
> >
> >
>
>


v-jetan

2/8/2004 10:14:00 AM

0


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.