asp.net - 获取单选按钮组中所选项目的文本
我找到了这段代码,但我想知道是否有更简化的方法来做到这一点。
例如,您可以只写一行 Label1.text = "You selected " & ,而不是使用所有 if 语句。 RadioGroup1.Text
Sub SubmitBtn_Click(Sender As Object, e As EventArgs)
If Radio1.Checked Then
Label1.Text = "You selected " & Radio1.Text
ElseIf Radio2.Checked Then
Label1.Text = "You selected " & Radio2.Text
ElseIf Radio3.Checked Then
Label1.Text = "You selected " & Radio3.Text
End If
End Sub
和
<asp:RadioButton id=Radio1 Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" />
<asp:RadioButton id=Radio2 Text="Compact" GroupName="RadioGroup1" runat="server"/>
<asp:RadioButton id=Radio3 Text="Full" GroupName="RadioGroup1" runat="server" />
<asp:Button text="Submit" OnClick="SubmitBtn_Click" runat=server/>
<asp:Label id=Label1 Font-Bold="true" runat="server" />
I've found this code, but I was wondering whether there's a more streamlined way to do it.
So for example, rather than having all the if statements can you have one line that says Label1.text = "You selected " & RadioGroup1.Text
Sub SubmitBtn_Click(Sender As Object, e As EventArgs)
If Radio1.Checked Then
Label1.Text = "You selected " & Radio1.Text
ElseIf Radio2.Checked Then
Label1.Text = "You selected " & Radio2.Text
ElseIf Radio3.Checked Then
Label1.Text = "You selected " & Radio3.Text
End If
End Sub
and
<asp:RadioButton id=Radio1 Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" />
<asp:RadioButton id=Radio2 Text="Compact" GroupName="RadioGroup1" runat="server"/>
<asp:RadioButton id=Radio3 Text="Full" GroupName="RadioGroup1" runat="server" />
<asp:Button text="Submit" OnClick="SubmitBtn_Click" runat=server/>
<asp:Label id=Label1 Font-Bold="true" runat="server" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 id 为
radio1
的
。然后在那里有单独的
。为每个列表项设置不同的值
,即典型、接触、完整。那么您在SubmitBtn_Click
中需要的就是radio1.SelectedItem.Value
You can use a
<asp:radiobuttonlist>
have it's id asradio1
. Then have seperate<asp:listitem>
in there. Set each list item with a differentvalue
, ie, Typical, Contact, Full. Then all you would need inSubmitBtn_Click
would beradio1.SelectedItem.Value
RadioButtonList 将有助于减少代码:
The RadioButtonList will help reduce code: