Programatically Setting DropDownList / RadioButtonList

Posted on: Tue Sep 09 10:32:59 -0700 2008. Updated on: Tue Sep 09 10:37:01 -0700 2008.
Category: DotNet

So, often we have a radio button list or drop down list that we want to be selected to a certain item when the page loads. You will notice that the only available setter property is the RadioButtonList1.SelectedIndex. It is possible to do set it to the correct SelectedValue using the following code behind (assuming you List has items or is databound):

ListItem item = RadioButtonList1.Items.FindByValue("value");
if (item != null)
{
    RadioButtonList1.SelectedIndex = RadioButtonList1.Items.IndexOf(item);
}

Replace the "value" string for whatever value it is you want selected. The "value" must be a valid value item in the drop down or radio button list.

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.