0

I have a large RadioButtonList (about 20 items) and need to check which one was selected in order to return that value with the submitted email (it's an email form).

Right now I'm running a large If-Else-If loop to parse through each RadioButton to find out which one was selected:

           if (PriMsg_AP_1.Checked)
            {
                message.Body += "<b>Primary Message:</b> " + PriMsg_AP_1.Text;
            }
            else if (PriMsg_AP_2.Checked)
            {
                message.Body += "<b>Primary Message:</b> " + PriMsg_AP_2.Text;
            }
            else if (PriMsg_Devices_1.Checked)
            {
                message.Body += "<b>Primary Message:</b> " + PriMsg_Devices_1.Text;
            }
            else if (PriMsg_SMB_1.Checked)
            {
                message.Body += "<b>Primary Message:</b> " + PriMsg_SMB_1.Text;
            }
            else if (PriMsg_SMB_2.Checked)
            {
                message.Body += "<b>Primary Message:</b> " + PriMsg_SMB_2.Text;
            }
            else if (PriMsg_SMB_3.Checked)
            {
                message.Body += "<b>Primary Message:</b> " + PriMsg_SMB_3.Text;
            }
            else if (PriMsg_Vertical_1.Checked)
            {
                message.Body += "<b>Primary Message:</b> " + PriMsg_Vertical_1.Text;
            }
            else if (PriMsg_Vertical_2.Checked)
            {
                message.Body += "<b>Primary Message:</b> " + PriMsg_Vertical_2.Text;
            }

etc.

As you can see it's pretty long and to me I would think there is an easier way to parse this list. Like running everything through a For loop? But not sure how to do that since each RadioButton has a different name...

Some caveats: The list is broken under multiple categories so not all RadioButtons are in one screen so I have the whole list tied with a GroupName so that the user can only select one item.

Thoughts? Suggestions?

I'm working in ASP.NET on this project.

~Allen

Edit: I'm using individual RadioButtons and tying them together with a GroupName. Not using a RadioButtonList. Sorry about the confusion.

<asp:RadioButton ID="PriMsg_AP_1" GroupName="PriMsg" runat="server" text="Promo 1" CssClass="radiobutton" />
Valien
  • 1,125
  • 2
  • 17
  • 38
  • 1
    related: http://stackoverflow.com/questions/5732540/finding-the-selected-radiobuttons-value-in-asp-net tried that? – naveen Aug 03 '11 at 15:27
  • naveen - looks promising. Will attempt that this afternoon and let you know. – Valien Aug 03 '11 at 15:34
  • It doesn't look like RadioButtonList. Are you using a list of RadioButtons? If RadiobuttonList, you can use RadioButtonList1.SelectedItem.Value – Win Aug 03 '11 at 15:35
  • Yeah, just realized my typo. Not using a RBList just RB's with a GroupName tied together. – Valien Aug 03 '11 at 15:44

3 Answers3

2

You can try using radio button list.

    "rlist1.SelectedItem.Text in code behind"

will give you the seleted radio button text.

    <asp:RadioButtonList runat="server" ID="rlist1">
        <asp:ListItem Text="a">
        </asp:ListItem>
        <asp:ListItem Text="b">
        </asp:ListItem>
    </asp:RadioButtonList>
Novice
  • 2,447
  • 3
  • 27
  • 33
1

You could use the conditional operator, which would make the code a lot shorter:

message.Body += "<b>Primary Message:</b> " +
  PriMsg_AP_1.Checked ? PriMsg_AP_1.Text :
  PriMsg_AP_2.Checked ? PriMsg_AP_2.Text :
  PriMsg_Devices_1.Checked ? PriMsg_Devices_1.Text :
  PriMsg_SMB_1.Checked ? PriMsg_SMB_1.Text :
  PriMsg_SMB_2.Checked ? PriMsg_SMB_2.Text :
  PriMsg_SMB_3.Checked ? PriMsg_SMB_3.Text :
  PriMsg_Vertical_1.Checked ? PriMsg_Vertical_1.Text :
  PriMsg_Vertical_2.Checked ? PriMsg_Vertical_2.Text :
  "No message";

Another alternative would be to put all the radio buttons in a collection so that you can loop through them:

RadioButton[] radios = new RadioButton[] {
  PriMsg_AP_1, PriMsg_AP_2, PriMsg_Devices_1,
  PriMsg_SMB_1, PriMsg_SMB_2, PriMsg_SMB_3,
  PriMsg_Vertical_1, PriMsg_Vertical_2
};
foreach (RadioButton radio in radios) {
  if (radio.Checked) {
    message.Body += "<b>Primary Message:</b> " + radio.Text;
    break;
  }
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • That's pretty interesting. It would be shorter for sure. Might give it a whirl and see how it works. – Valien Aug 03 '11 at 15:36
  • I used your 2nd option and worked great. Reduces the amount of if-then-else statements and cleans it up a little. Thanks! – Valien Aug 03 '11 at 21:02
0

I suggest that you put an asp panel that surrounds them all and loop on this panel using the following code

foreach (Control c in myPanel.Controls)
            {
                if(c.GetType() == typeof(RadioButton))
                {
                    if(((RadioButton)c).Checked)
                    {
                        message.Body += "<b>Primary Message:</b> " + ((RadioButton)c).Text;
                    }
                }
            }

this will sure work but it can be enhanced if you are not using a master page you can simply replace the mpPane.Controls with this.Controls

Samir Adel
  • 2,691
  • 2
  • 15
  • 16