CJ

The circle of life – ** .Net – Training – MOSS **

  • Past Post

  • Vistor Locations

DropDownList FindByText FindByValue

Posted by Clayton James on November 27, 2008

Am always looking for this code snippet.

If you try to do this, you will not achieve the desire results:

 

ddlList1.SelectedItem.Text = dropdownValue;

 

By doing that, it will overwrite existing items. Not a good idea at all. Try this instead; you can either set FindByText or FindByValue.

 

ddlList1.SelectedIndex = ddlList1.Items.IndexOf(ddlList1.Items.FindByText(dropdownText));

 

Or

 

ddlList1.SelectedIndex = ddlList1.Items.IndexOf(ddlList1.Items.FindByValue(dropdownValue));

Advertisements

One Response to “DropDownList FindByText FindByValue”

  1. Don Draper said

    Be very careful using IndexOf to locate an list item. If you add a another item to the list after databinding such as for a blank option or one that says “Pick One”, you not get the expected results.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: