Link to home
Start Free TrialLog in
Avatar of Tchami
Tchami

asked on

foreach RadioButton

Hi,

I'm coding a pools coupon and for that I've created a Panel and inside that I have 3 x 13 RadioButtons, it looks something like this:

<asp:Panel ID="plTip" Runat="server">

<asp:RadioButton id="match1_1" GroupName="match1" value="1" Runat="server"
/>
<asp:RadioButton id="match1_x" GroupName="match1" value="x" Runat="server"
/>
<asp:RadioButton id="match1_2" GroupName="match1" value="2" Runat="server"
/>

<asp:RadioButton id="match2_1" GroupName="match2" value="1" Runat="server"
/>
<asp:RadioButton id="match2_x" GroupName="match2" value="x" Runat="server"
/>
<asp:RadioButton id="match2_2" GroupName="match2" value="2" Runat="server"
/>

etc.
<asp:Button OnClick="Submit" cssclass="submit-button" text="  Tip  " runat="Server"/>

</asp:Panel>

When a user clicks the submit button I need to retrieve the selected value from each of the 13 groups. I could have done 13 x Request.Form["GroupName"] but that seems a bit overkill when I'm sure there's a much simpler way to do this but this is what's teasing me. Here's the code I've done so far:

public void Submit(object sender, System.EventArgs e)
{
      foreach(Control btn in plTip.Controls)
      {
            if (btn is System.Web.UI.WebControls.RadioButton)
            {
                  lblStatus.Text += ((RadioButton)btn).Text + "<br>";
            }
      }
}

This doesn't work however as I get no output whatsoever when hitting the submit button.

Anyone got a solution?
Avatar of YZlat
YZlat
Flag of United States of America image

public void Submit(object sender, System.EventArgs e)
{
     foreach(Control btn in plTip.Controls)
     {
          if (btn is System.Web.UI.WebControls.RadioButton)
          {
               lblStatus.Text += ((RadioButton)btn).SelectedItem.Text + "<br>";
          }
     }
}
Avatar of Tchami
Tchami

ASKER

SelectedItem only applies to the RadioButtonList class so that doesn't work I'm afraid. Visual Studio .NET reports this error:

"'System.Web.UI.WebControls.RadioButton' does not contain a definition for 'SelectedItem'"

Avatar of Tchami

ASKER

public void Submit(object sender, System.EventArgs e)    
{    
     foreach(Control btn in plTip.Controls)    
     {    
          if (btn is System.Web.UI.WebControls.RadioButton)    
          {    
               lblStatus.Text += ((RadioButton)btn).ID + " - " + ((RadioButton)btn).Checked.ToString() + "<br>";    
          }    
     }    
}

seems to do the trick :)
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial