FindByText dropdownlist

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top