Ich habe eine Abstimmung für ein Wahlsystem und ich muss alle Kontrollkästchen auf dem Stimmzettel durchlaufen, um zu sehen, ob sie aktiviert oder deaktiviert sind. Sie können zum Beispiel nur 1 der 5 verfügbaren Boxen auswählen. Ich stecke fest und kann nicht für das Leben von mir herausfinden. Der folgende Code ist meine Funktion, die ausgeführt wird, wenn der Benutzer auf die Schaltfläche Senden klickt.Loop Checkbox IDs, um zu sehen, ob alle überprüft werden
Dieser Code funktioniert und reicht meine Abstimmung ein, überprüft aber nicht die Anzahl der Checkboxen.
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")
Ich habe ein paar Dinge hinzugefügt, um zu versuchen und diese richtig zu laufen zu bekommen, aber kein Glück, mein Code zur Zeit die folgenden Ergebnisse.
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
Jede Hilfe würde geschätzt werden, und danke im Voraus.
Direct auch als nützlich erweisen könnten. –