ASP.NET 2.0 DropDownList - FindByText / FindByValue

Assuming you want to achieve this:

 

1 GridView with 2 columns of databoundfield. There is 1 buttonfield called Edit with a command “UpdateThis”. When you click on Edit, it will open an input panel and shows 2 dropdown list controls. The 1st dropdown list has 5 items inside. The 2nd one has 10 items inside. From the gridview’s values selected, you need to dynamic pass the values into the dropdownlist while maintaining the state of it. How can you do that?

 

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

 

ddlProgramOwnerDivisionName.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));

 

Have fun.

Published Monday, November 06, 2006 3:56 PM by chuawenching

Comments

# Luciano

Wednesday, August 06, 2008 6:52 PM by Lorenzo

81a9d7eff6acc35575b64a29c680733d

<a href="njdokj.info/.../81a9d7eff6acc35575b64a29c680733d">">njdokj.info/.../81a9d7eff6acc35575b64a29c680733d"> njdokj.info/.../81a9d7eff6acc35575b64a29c680733d </a>

njdokj.info/.../81a9d7eff6acc35575b64a29c680733d

[url]njdokj.info/.../81a9d7eff6acc35575b64a29c680733d[url]