循环选中复选框ID以查看是否已选中任何内容

时间:2017-06-13 13:50:40

标签: asp.net vb.net

我有一个选举系统的选票,我需要循环选票上的所有复选框,看看它们是否被选中或未选中。您只能选择(例如)可用的5个框中的1个。我被困住了,不能为我的生活搞清楚这一点。以下代码是我在用户单击提交按钮时运行的函数。

此代码可以投票并提交我的选票,但不会检查选中的复选框的数量。

For Each row As Object In candidatesTable.Rows
    If row(1) = ballot_ID Then
        Dim checkBox_ID = row(0)
        Dim CB As New CheckBox()
        CB = mainBallotDiv.FindControl(checkBox_ID)

        If CB.Checked Then
            Dim addVote As Integer = row("votes")
            addVote += 1
            candidatesAdapter.addVoteToCandidate(addVote, row(0))
            Dim section_ID As Integer = row(2)
            Dim voter As String = userGnumber
            Dim vote As Integer = checkBox_ID
            Dim hasVoted As Boolean = True
            votesAdapter.InsertVotes(ballot_ID, section_ID, voter, vote, hasVoted)
        End If
    End If
Next
Response.Redirect("~/voting/voted.aspx")

我已经添加了一些东西来尝试让它运行正常但没有运气,我的代码目前如下。

Dim checkedCount As Integer
    For Each row As Object In candidatesTable.Rows
        If row(1) = ballot_ID Then
            Dim checkBox_ID = row(0)
            Dim CB As New CheckBox()
            CB = mainBallotDiv.FindControl(checkBox_ID)
            Dim section_idFromCB As Integer = candidatesAdapter.getsectionIDfromcandidateID(CB.ID)
            Dim voteLimit As Integer = sectionsAdapter.votesbysectionid(section_idFromCB)

            If CB.Checked Then
                checkedCount += 1
                Debug.Write(checkedCount)
                If checkedCount > voteLimit Then
                    ' error
                    Response.Write("<script language=""javascript"">alert('You can not select that many check boxes.');</script>")
                    Response.Redirect(Request.RawUrl)

                Else
                    ' pass

                    For Each Nrow As Object In candidatesTable.Rows
                        If Nrow(1) = ballot_ID Then
                            Dim NcheckBox_ID = row(0)
                            Dim NCB As New CheckBox()
                            NCB = mainBallotDiv.FindControl(NcheckBox_ID)
                            If NCB.Checked Then
                                Dim addVote As Integer = row("votes")
                                addVote += 1
                                candidatesAdapter.addVoteToCandidate(addVote, row(0))
                                Dim section_ID As Integer = row(2)
                                Dim voter As String = userGnumber
                                Dim vote As Integer = checkBox_ID
                                Dim hasVoted As Boolean = True
                                votesAdapter.InsertVotes(ballot_ID, section_ID, voter, vote, hasVoted)
                            End If
                        End If
                    Next
                    Response.Redirect("~/voting/voted.aspx")
                End If
            End If
        End If
    Next

任何帮助将不胜感激,并提前感谢。

2 个答案:

答案 0 :(得分:1)

这是我的推荐......

您可以将它们放在required中,然后您可以根据需要随时访问它们,并获得您需要的任何属性。

List(Of CheckBox)

Dim lstChecked As New List(Of CheckBox) lstChecked = divcontrol.Controls.OfType(Of CheckBox).Where(Function(ch) ch.Checked = True).ToList 可以是要检查的任何lstChecked ...

答案 1 :(得分:-2)

For Each checkBox In Me.Controls.OfType(Of CheckBox)
     ' do something
Next