views:

268

answers:

1

hey all,

i have a table which contains a bunch of dynamically created radio button lists, im trying to write code which will loop through each one of the radio button list and get the text value of the selected item. i have the following code

   foreach ( Control ctrl in Table1.Controls)
    {
        if (ctrl is RadioButtonList)
        {
           //get the text value of the selected radio button 
        }
    }

but i am stuck on how i can get the value of the selected item for that given control.

+2  A: 

Try this:

foreach (Control ctrl in Table1.Controls)
{
    if (ctrl is RadioButtonList)
    {  
        RadioButtonList rbl = (RadioButtonList)ctrl;

        for (int i = 0; i < rbl.Items.Count; i++)
        {
            if (rbl.Items[i].Selected)
            {
                //get the text value of the selected radio button
                string value = rbl.Items[i].Text;
            }
        }
    }
}

To determine the selected items in the RadioButtonList control, iterate through the Items collection and test the Selected property of each item in the collection.

Look here: RadioButtonList Web Server Control

Leniel Macaferi
Error 1 'System.Web.UI.Control' does not contain a definition for 'Items' and no extension method 'Items' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?) C:\Users\C!\Documents\My Dropbox\Final Year\Project\ASP\WebSite2\Default5.aspx.cs 61 42 C:\...\WebSite2\
c11ada
cast ctrl to radionbuttonlist inside the loop `for (int i = 0; i < ((RadioButtonList )ctrl).Items.Count; i++)`
fearofawhackplanet
@c11ada: I forgot to cast crtl to RadioButtonList. While I was correcting the code fearofawhackplanet wrote the comment above. Telepathy. :)
Leniel Macaferi