asp.net - 获取单选按钮组中所选项目的文本

发布于 2024-10-15 01:21:19 字数 961 浏览 5 评论 0原文

我找到了这段代码,但我想知道是否有更简化的方法来做到这一点。

例如,您可以只写一行 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 技术交流群。

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

发布评论

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

评论(3

独孤求败 2024-10-22 01:21:19

您可以使用 id 为 radio1。然后在那里有单独的 。为每个列表项设置不同的,即典型、接触、完整。那么您在 SubmitBtn_Click 中需要的就是 radio1.SelectedItem.Value

You can use a <asp:radiobuttonlist> have it's id as radio1. Then have seperate <asp:listitem> in there. Set each list item with a different value, ie, Typical, Contact, Full. Then all you would need in SubmitBtn_Click would be radio1.SelectedItem.Value

我三岁 2024-10-22 01:21:19

RadioButtonList 将有助于减少代码:

<p>
    <strong>Output:</strong>
    <asp:Label runat="server" ID="lOut" />
</p>
<asp:RadioButtonList runat="server" ID="rblist1" AutoPostBack="true"     onselectedindexchanged="rblist1_SelectedIndexChanged"  >
    <asp:ListItem Value="1">One</asp:ListItem>
    <asp:ListItem Value="2">Two</asp:ListItem>
    <asp:ListItem Value="3">Three</asp:ListItem>
</asp:RadioButtonList>


protected void rblist1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.lOut.Text = string.Format("RadioButton: selected: {0}={1} ", rblist1.SelectedItem.Text, rblist1.SelectedValue);
}

The RadioButtonList will help reduce code:

<p>
    <strong>Output:</strong>
    <asp:Label runat="server" ID="lOut" />
</p>
<asp:RadioButtonList runat="server" ID="rblist1" AutoPostBack="true"     onselectedindexchanged="rblist1_SelectedIndexChanged"  >
    <asp:ListItem Value="1">One</asp:ListItem>
    <asp:ListItem Value="2">Two</asp:ListItem>
    <asp:ListItem Value="3">Three</asp:ListItem>
</asp:RadioButtonList>


protected void rblist1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.lOut.Text = string.Format("RadioButton: selected: {0}={1} ", rblist1.SelectedItem.Text, rblist1.SelectedValue);
}
因为看清所以看轻 2024-10-22 01:21:19
string valueName = Radio3.Text;
string valueName = Radio3.Text;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文