代码之家  ›  专栏  ›  技术社区  ›  JayyCodez

只允许选中一些复选框。加载到page_load

  •  0
  • JayyCodez  · 技术社区  · 9 年前

    我正在根据db表中的行数(按ID)动态地将asp复选框添加到我的页面中。此外,正在从db表中为复选框分配ID。我还在db表中的两列“numberOffered”和“numberAllowed”。我的想法是页面加载只允许用户选中显示的10个复选框中的3个。我删除了很多我认为不必要的代码。事先非常感谢。

    For Each Arow As Object In ATable.Rows
        For Each Brow As Object In BTable.Rows
            If Brow(1) = a_ID Then
                If Brow(2) = b_ID Then
                    Dim cbShown As Integer = Arow(5)
                    Dim cbAllowed As Integer = Arow(6)
                    Dim checkBox As New CheckBox()
                End If
            End If
        Next
    Next
    
    checkBox.ID = Crow(0)
    divcontrol.Controls.Add(checkBox)
    

    编辑: 整页加载子

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not (Session("studentLoggedIn") Or Session("adminLoggedIn")) Then
                Routines.LogOut()
            End If
    
            If Session("adminLoggedIn") = True Then
                castVote.Enabled = False
                castVote.CssClass = "btnDisabled"
                Dim p As New HtmlGenericControl()
                p.TagName = "p"
                p.InnerText = "Vote button disabled. Only students may vote."
                adminMsg.Controls.Add(p)
            End If
    
            Dim ballot_ID As Integer = CType(Session.Item("ballot_ID"), Integer)
            Dim ballotName As String = CType(Session.Item("ballotName"), String)
    
            Dim ballotsAdapter As New eVoteTableAdapters.ballotsTableAdapter()
            Dim ballotsTable As New eVote.ballotsDataTable
            ballotsTable = ballotsAdapter.GetDataBy3getBallotsByID(ballot_ID)
    
            Dim sectionsAdapter As New eVoteTableAdapters.sectionsTableAdapter()
            Dim sectionsTable As New eVote.sectionsDataTable
            sectionsTable = sectionsAdapter.GetDataBygetsectionsByBallotID(ballot_ID)
    
            Dim candidatesAdapter As New eVoteTableAdapters.candidatesTableAdapter()
            Dim candidatesTable As New eVote.candidatesDataTable
            candidatesTable = candidatesAdapter.GetDataBygetCandidatesByballotID(ballot_ID)
    
            openBallotName.InnerText = ballotName
    
            Dim section_ID
            For Each row As Object In sectionsTable.Rows
                If row(1) = ballot_ID Then
                    section_ID = row(0)
                    Dim sectionName As New HtmlGenericControl()
                    Dim sectionDescription As New HtmlGenericControl()
                    Dim divcontrol As New HtmlGenericControl()
                    Dim br As New HtmlGenericControl()
                    divcontrol.Attributes("ID") = section_ID
                    divcontrol.Attributes("runat") = "server"
                    divcontrol.Attributes("style") = "border: solid;"
                    divcontrol.TagName = "div"
                    br.TagName = "br"
                    sectionName.TagName = "h4"
                    sectionDescription.TagName = "p"
                    mainBallotDiv.Controls.Add(divcontrol)
                    mainBallotDiv.Controls.Add(br)
                    sectionName.InnerText = row(2)
                    sectionDescription.InnerText = row(3)
                    divcontrol.Controls.Add(sectionName)
                    divcontrol.Controls.Add(sectionDescription)
    
    
                    For Each Crow As Object In candidatesTable.Rows
                        If Crow(1) = ballot_ID Then
                            If Crow(2) = section_ID Then
                                Dim checkBox As New CheckBox()
                                Dim canImg As New Image()
                                Dim canName As New HtmlGenericControl()
                                Dim canBio As New HtmlGenericControl()
                                Dim rmImg As New Image()
                                Dim rmName As New HtmlGenericControl()
                                Dim rmBio As New HtmlGenericControl()
                                Dim canBytes As Byte() = Crow(6)
                                Dim canBase64String As String = Convert.ToBase64String(canBytes, 0, canBytes.Length)
                                Dim rmBytes As Byte() = Crow(11)
                                Dim rmBase64String As String = Convert.ToBase64String(rmBytes, 0, rmBytes.Length)
                                checkBox.ID = Crow(0)
                                canName.TagName = "h3"
                                canBio.TagName = "p"
                                rmName.TagName = "h3"
                                rmBio.TagName = "p"
                                canName.InnerText = Crow(4) & " " & Crow(5)
                                canBio.InnerText = Crow(7)
                                canImg.ImageUrl = Convert.ToString("data:image/png;base64,") & canBase64String
                                canImg.Height = 120
                                rmName.InnerText = Crow(9) & " " & Crow(10)
                                rmBio.InnerText = Crow(12)
                                rmImg.ImageUrl = Convert.ToString("data:image/png;base64,") & rmBase64String
                                rmImg.Height = 120
                                divcontrol.Controls.Add(checkBox)
                                divcontrol.Controls.Add(canImg)
                                divcontrol.Controls.Add(canName)
                                divcontrol.Controls.Add(canBio)
                                If row(4) = True Then
                                    divcontrol.Controls.Add(rmImg)
                                    divcontrol.Controls.Add(rmName)
                                    divcontrol.Controls.Add(rmBio)
                                End If
                            End If
                        End If
                    Next
                End If
            Next
        End Sub
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Kevin Talha Berk Yamanoglu    9 年前

    您需要一个变量(整数)表示允许的复选框数量,然后是另一个变量表示当前选中的复选框的数量,最后是一个包含每个复选框名称的列表,(将所有这些变量作为类字段)

    然后在事件处理程序中

    Sub Check_Clicked(sender As Object, e As EventArgs)
    
    checked += 1        
            If checked >= NumberAllowedChecked Then
                For Each a As CheckBox In MyCheckBoxList
                       If Not CheckBox.Checked Then CheckBox.Enabled = False               
                    Next
            End If
    End Sub
    

    如果用户取消选中一个复选框,则需要添加逻辑,该复选框将从“选中”中减去一个;

    推荐文章