【问题标题】:asp.net - get the text of the selected item in a radiobutton groupasp.net - 获取单选按钮组中所选项目的文本
【发布时间】:2011-06-19 05:46:33
【问题描述】:

我找到了这段代码,但我想知道是否有更简化的方法来执行此操作。

因此,例如,与其拥有所有 if 语句,您可以在一行中显示 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

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

【问题讨论】:

    标签: asp.net vb.net radio-button groupname


    【解决方案1】:

    您可以使用&lt;asp:radiobuttonlist&gt; 将其ID 设为radio1。然后在那里有单独的&lt;asp:listitem&gt;。为每个列表项设置不同的value,即典型、联系人、完整。那么你在SubmitBtn_Click 中所需要的就是radio1.SelectedItem.Value

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:
        string valueName = Radio3.Text;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-23
          • 2017-02-15
          • 2013-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多