Sunday, January 15, 2012

Dynamically setting the selected value in List-based controls

If you ever have the need to dynamically add controls to your screen or to simply set the selected value in a List-based control such as a DropDownList, CheckBoxList or RadioButtonList, you can follow the example below:

Essentially, the crux of the code that accomplishes this for you is the following:

RadioButtonList1.Items.FindByText(searchString).Selected = true;

You can alternatively also use the .FindByValue method if that suits your needs better.

No comments:

Post a Comment