FindByText dropdownlist

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am using FindByText method of dropdownlist to change the selected item in a dropdownlist like this:

DropDownList1.Items("mySelectedText").Selected = True

It doesn't seem to be doing the trick. I have this piece of code in OnSelectedIndexChanged event of the dropdownlist.

Can anyone tell me why it's not setting the selected item?

Thanks,
AK
 
The DropDownList's Items collection does not accept a string or a ListItem name as an argument. If you only know the text of the item you wish to have selected you can use:

DropDownList1.SelectedValue = DropDownList1.Items.FindByText("text").Value

hope this helps,
John
 
HI John,

Thanks for responding..But in a hurry, I think I missed typing FindByText.
Instead of setting DropDownList1.Items.FindByText("mySelectedText").Selected = True, I also tried the exact same line of code that you gave below. But the surprising and as well as annoying thing that I noticed is that, if I hardcode the text parameter, it works fine but if I have that value in a variable, it doesn't set the selected value. I also tried trimming the text before passing it to the FindByText method. I even printed out the variable value to the browser and it's exact same thing as I hardcoded.

Could you tell me what could be the reason for this?

Thanks,
AK
 
Hi AK,

No if the values are the same and you are sure of that, then there is no reason that it shouldn't work. Try something like this.

Dim strValue as String = "text"
DropDownList1.SelectedValue = DropDownList1.Items.FindByText(strValue).Value

and then try replacing strValue with "text".. They should both work. If it doesn't please post some code; there has to be something going on!

thanks,
John
 
Back
Top