如何检查单选按钮是否在组中被选中

发布于 2024-09-13 20:48:35 字数 727 浏览 3 评论 0原文

我有一组单选按钮,每组有 5 - 27 个单选按钮。如果选中了组中的任何单选按钮,我会将 1 存储在数据库中,否则会存储 0。现在,我使用 if 循环检查每个单选按钮,以查看它们是否已选中并设置数据库值。我也在尝试使用下面的代码。是否有一个好的/更好的方法来检查它们是否被检查?

当前代码:

'rname is radiobutton prefix for a given group
'cnt is number of radiobuttons in the group

Private Function RadioIsChecked(ByVal rname As String, ByVal cnt As Integer) As Integer
    Dim retval As Integer = 0
    For i = 0 To cnt - 1
        Dim rdbName As String = rname & i
        Dim rdb As New RadioButton()
        rdb = CType(Me.Page.FindControl(rdbName), RadioButton)
        If rdb.Checked Then
            retval = 1
        End If
    Next
    Return retval
End Function

注意:我无法使用单选按钮列表。我知道使用它可以轻松实现这一点,但我想获得单选按钮的解决方案

I have group of radio buttons, each group ranges from 5 - 27 radiobuttons. And if any radio button in a group is checked I store 1 in db else I store 0. Now I'm checking each radiobutton using if loop to see if they are checked and set database value. I'm also trying to use the code below. Is there a good/better approach to check if they are checked or not?

Current code:

'rname is radiobutton prefix for a given group
'cnt is number of radiobuttons in the group

Private Function RadioIsChecked(ByVal rname As String, ByVal cnt As Integer) As Integer
    Dim retval As Integer = 0
    For i = 0 To cnt - 1
        Dim rdbName As String = rname & i
        Dim rdb As New RadioButton()
        rdb = CType(Me.Page.FindControl(rdbName), RadioButton)
        If rdb.Checked Then
            retval = 1
        End If
    Next
    Return retval
End Function

Note: I cannot use radio button list. I know this can be achieved easily using this but i want to get solution for radiobutton

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

你列表最软的妹 2024-09-20 20:48:35

无论哪种方式,都取决于你。

只有在找到选中的项目后退出迭代,您才会获得所需的性能提升。

您可以按如下方式修改迭代:

For i = 0 To cnt - 1
    Dim rdbName As String = rname & i
    Dim rdb As New RadioButton()
    rdb = CType(Me.Page.FindControl(rdbName), RadioButton)
    If rdb.Checked Then
        retval = 1
        Exit For
    End If
Next

Either way, it's up to you.

You'll get the needed performance boost only if you exit the iteration after finding the checked item.

You can modify the iteration as follows:

For i = 0 To cnt - 1
    Dim rdbName As String = rname & i
    Dim rdb As New RadioButton()
    rdb = CType(Me.Page.FindControl(rdbName), RadioButton)
    If rdb.Checked Then
        retval = 1
        Exit For
    End If
Next
不甘平庸 2024-09-20 20:48:35

您还可以使用 Request.Form("GroupNameGoesHere") 获取该组中当前选定的单选按钮的值(如果不存在,则获取空字符串)。

You can also use Request.Form("GroupNameGoesHere") to get the value of the currently selected radio button in that group (or an empty string if none exist).

垂暮老矣 2024-09-20 20:48:35

将其改为 RadioButtonList。然后您可以简单地检查列表的“SelectedItem”属性。

看看 http://www.startvbdotnet.com/aspsite/controls/rblist.aspx< /a> 为例。

Make it a RadioButtonList instead. Then you can simply check the "SelectedItem" property of the List.

Look at http://www.startvbdotnet.com/aspsite/controls/rblist.aspx for examples.

暖心男生 2024-09-20 20:48:35

如果您使用如上所述的单选按钮列表,您可以执行类似的操作。测试是否选择了某些内容。

<asp:RadioButtonList runat="server" ID="rbList">
      <asp:ListItem Text="Radio 1" />
      <asp:ListItem Text="Radio 2" />
      <asp:ListItem Text="Radio 3" />
</asp:RadioButtonList>


rbList.SelectedIndex > -1;

If you use a radiobutton list as mentioned you could do something like this. To test if something was selected.

<asp:RadioButtonList runat="server" ID="rbList">
      <asp:ListItem Text="Radio 1" />
      <asp:ListItem Text="Radio 2" />
      <asp:ListItem Text="Radio 3" />
</asp:RadioButtonList>


rbList.SelectedIndex > -1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文