遍历复选框ID以查看是否已选中 [英] Loop through checkbox ID's to see if any are checked

查看:51
本文介绍了遍历复选框ID以查看是否已选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选举系统的选票,我需要遍历选票上的所有复选框以查看它们是否已选中.您只能选择(例如)5个框中的1个.我被困住了,无法为我的一生解决这个问题.以下代码是我在用户单击提交"按钮时运行的功能.

I have a ballot for a election system and I need to loop through all the checkboxes on the ballot to see if they are checked or unchecked. You can only select (for example) 1 of the 5 boxes available. I am stuck and can not for the life of me figure this out. The following code is my function that runs when the user clicks the submit button.

此代码可以正常工作并提交我的投票,但不检查已选中复选框的数量.

This code works and submit my ballot but does not check the number of checkboxes checked.

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

我添加了一些方法来尝试使其正常运行,但是没有运气,我的代码如下.

I have added a couple things to try and get this to run correctly but no luck, my code currently is the following.

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

我们将不胜感激,在此先感谢您.

Any help would be appreciated, and thanks in advance.

推荐答案

这是我的建议...

您可以将它们放在 List(Of CheckBox)中,然后可以根据需要随时访问它们,并获取所需的任何属性.

You can put them in a List(Of CheckBox) then you can access them anytime as needed as well as get any property you would need.

Dim lstChecked As New List(Of CheckBox)
lstChecked = divcontrol.Controls.OfType(Of CheckBox).Where(Function(ch) ch.Checked = True).ToList

lstChecked 是将要检查的任何 CheckBox ...

lstChecked would be any CheckBox that would be checked...

这篇关于遍历复选框ID以查看是否已选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆