有没有比 If-Else-If 循环更好的方法来读取 RadioButtonList?
我有一个很大的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用单选按钮列表。
将为您提供选定的单选按钮文本。
You can try using radio button list.
will give you the seleted radio button text.
您可以使用条件运算符,这将使代码变得更短:
另一种选择是将所有单选按钮放在一个集合中,以便您可以循环遍历它们:
You could use the conditional operator, which would make the code a lot shorter:
Another alternative would be to put all the radio buttons in a collection so that you can loop through them:
我建议您放置一个围绕它们的 asp 面板,并使用以下代码在该面板上循环,
这肯定可以工作,但如果您不使用母版页,则可以对其进行增强,您可以简单地将 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
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