如何检查是否一个单选按钮在一组检查单选、按钮

2023-09-04 04:57:27 作者:你别皱眉

我有组单选按钮,从5各组范围 - 27单选按钮。如果被选中的组中的任何单选按钮,我店1分贝否则我存储0。现在,我使用,如果循环,看看他们进行检查并设置数据库值检查每一个单选按钮。我也尝试使用下面的code。是否有一个良好/更好的方法来检查自己是否选中或不?

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?

目前的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

推荐答案

无论哪种方式,它给你。 你会得到所需的性能提升只有当您退出迭代找到检查项目之后。

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.

您可以修改迭代如下:

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