有没有比 If-Else-If 循环更好的方法来读取 RadioButtonList?

发布于 2024-11-27 17:53:46 字数 1966 浏览 1 评论 0原文

我有一个很大的 RadioButtonList(大约 20 个项目),需要检查选择了哪一个,以便通过提交的电子邮件(这是一个电子邮件表单)返回该值。

现在我正在运行一个大的 If-Else-If 循环来解析每个 RadioButton 以找出选择了哪个:

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

等等。

正如你所看到的,它相当长,对我来说,我认为有一种更简单的方法来解析它列表。就像通过 For 循环运行所有内容一样?但不确定如何做到这一点,因为每个 RadioButton 都有不同的名称...

一些注意事项:该列表在多个类别下被破坏,因此并非所有 RadioButton 都在一个屏幕中,因此我将整个列表与 GroupName 绑定在一起,以便用户可以仅选择一项。

想法?建议?

我正在使用 ASP.NET 来完成这个项目。

〜艾伦

编辑:我正在使用单独的单选按钮并将它们与组名绑定在一起。不使用 RadioButtonList。很抱歉造成混乱。

<asp:RadioButton ID="PriMsg_AP_1" GroupName="PriMsg" runat="server" text="Promo 1" CssClass="radiobutton" />

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦幻的心爱 2024-12-04 17:53:46

您可以尝试使用单选按钮列表。

    "rlist1.SelectedItem.Text in code behind"

将为您提供选定的单选按钮文本。

    <asp:RadioButtonList runat="server" ID="rlist1">
        <asp:ListItem Text="a">
        </asp:ListItem>
        <asp:ListItem Text="b">
        </asp:ListItem>
    </asp:RadioButtonList>

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>
沙沙粒小 2024-12-04 17:53:46

您可以使用条件运算符,这将使代码变得更短:

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

另一种选择是将所有单选按钮放在一个集合中,以便您可以循环遍历它们:

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

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;
  }
}
讽刺将军 2024-12-04 17:53:46

我建议您放置一个围绕它们的 asp 面板,并使用以下代码在该面板上循环,

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

这肯定可以工作,但如果您不使用母版页,则可以对其进行增强,您可以简单地将 mpPane.Controls 替换为 this.Controls

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文