我有一个大的 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 都有不同的名称......
一些警告:该列表分为多个类别,因此并非所有 RadioButtons 都在一个屏幕中,因此我将整个列表与 GroupName 绑定,以便用户只能选择一项。
想法?建议?
我在这个项目的 ASP.NET 中工作。
~艾伦
编辑:我正在使用单个 RadioButtons 并将它们与 GroupName 绑定在一起。不使用 RadioButtonList。很抱歉造成混乱。
<asp:RadioButton ID="PriMsg_AP_1" GroupName="PriMsg" runat="server" text="Promo 1" CssClass="radiobutton" />