开发者

Is there a better way to read a RadioButtonList than If-Else-If loops?

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" />


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>


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;
  }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜