【问题标题】:Getting Checked value for each item in a CheckBoxList获取 CheckBoxList 中每个项目的 Checked 值
【发布时间】:2011-10-26 09:16:33
【问题描述】:

我的网站上有一个用户可以选择的选项列表。我想要做的是提供功能来限制用户根据他在 CheckBoxList 中的选择获得的内容量。 一旦他选择了他想要的东西,他将单击保存,他的选择将被写入​​数据库。 CheckBoxList 最初是从 Modules 表中填充的。这提供了用户可以选择的模块列表。 当他单击 Save 时,代码需要遍历此 CheckBoxList 并“挑选”已选中的 CheckBox 的值,而忽略未选中的值。

问题在于,无论 CheckBox 是否被选中,调试器都会为 CheckBoxList.Items(i).Selected 属性返回一个 False 值。

这是我的代码:

注意:我知道这里的 SQL 代码容易受到注入攻击。有人告诉我,我不应该关心它,尽管通常情况下我会采取不同的做法。

Private Sub AddUpdateOrg(ByVal OrganizationName As String, ByVal Action As String, Optional ByVal Target As Integer = Nothing)
    ' ###############################################################
    ' #                ADD OR UPDATE AN ORGANIZATION                #
    ' # ============================================================#
    ' # PARAMETERS                                                  #
    ' # ----------                                                  #
    ' # OrganizationName As String                                  #
    ' # The organization name that is to be stored in the database. #
    ' #                                                             #
    ' # Action As String                                            #
    ' # What action will be taken by this procedure, add or update. #
    ' #                                                             #
    ' # Optional Target As Integer (Default: Nothing)               #
    ' # If updating an existing record, specify the OrganizationID  #
    ' # of the target organization.                                 #
    ' ###############################################################

    ' Get the list of Modules selected for this organization
    Dim ModuleList As New List(Of Integer)
    For i As Integer = 0 To chkModules.Items.Count - 1
        If chkModules.Items(i).Selected Then
            ModuleList.Add(chkModules.Items(i).Value)
        End If
    Next

    Dim sql As String = Nothing

    Select Case Action
        Case "Add"
            sql = "insert into Organizations(OrganizationName) values ('" & OrganizationName & "')"

        Case "Update"
            sql = "update Organizations set OrganizationName = '" & OrganizationName & "' " & _
                "where OrganizationID = " & Target
    End Select

    Dim dr As SqlDataReader = d.GetReader("select * from OrgModules where OrgID = " & Target)
    If dr.HasRows Then
        dr.Close()
        UpdateModules(Target, ModuleList, "update")
    Else
        dr.Close()
        UpdateModules(Target, ModuleList, "insert")
    End If
    d.DoCommand(sql)
End Sub

此行为可能是保存按钮回发的结果。如果是这种情况,我不确定如何解决它,因此将不胜感激。

编辑 1 在进一步检查代码后,我重新考虑了此问题是由回发引起的可能性,因为 CheckBoxList 未绑定到页面加载。

【问题讨论】:

  • 谁告诉过你不应该担心 SQL 注入攻击,这是错误的。
  • @Curt 正确,但与我的情况无关。

标签: asp.net vb.net checkboxlist


【解决方案1】:

在任何人都猜不到可行的解决方案之前就找到了解决自己问题的方法,这真是太糟糕了...

不过我修好了!

ModuleList 不需要在这里创建,而是在 .Click 事件处理程序中创建。它也没有在其余程序中进行。使用地点

我刚刚声明它从那里全局使用它。

我的疏忽大意。

【讨论】:

    猜你喜欢
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多