获取 CheckBoxList 中每个项目的选中值

发布于 2024-12-11 20:17:01 字数 2750 浏览 0 评论 0原文

我的网站上有一个用户可以选择的选项列表。我想要做的是提供功能来限制用户根据他在 CheckBoxList 中的选择获得的内容量。 一旦他选择了他想要的内容,他将单击“保存”,他的选择将被写入​​数据库。 CheckBoxList 最初是从模块表中填充的。这提供了用户可以选择的模块列表。 当他单击“保存”时,代码需要循环遍历此 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 未在页面加载时绑定。

I have a list of options on my site that a user can select. What I want to do is provide functionality to limit the amount of content the user gets based on his selection in a CheckBoxList.
Once he's selected what he wants, he'll click Save and his selection will be written to the database.
The CheckBoxList is initially populated from the Modules table. This provides a list of modules that the user can select.
When he clicks Save, the code needs to loop through this CheckBoxList and "pick out" the values for the CheckBoxes that were checked, disregarding the ones that weren't.

The problem is that whether a CheckBox is checked or not, the debugger returns a False value for the CheckBoxList.Items(i).Selected property.

Here's my code:

NOTE: I am aware that the SQL code here is open to injection attacks. I've been told that I shouldn't concern myself with it, although, ordinarily, I'd do it differently.

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

Its possible that this behavior is the result of a postback from the save button. If this is the case, I'm not sure how to fix it so any help will be greatly appreciated.

EDIT 1 Upon further examination of the code, I've reconsidered the possibility that this issue is caused by a postback as the CheckBoxList is not bound on page load.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄凉少年不暖心 2024-12-18 20:17:01

当你在别人猜出可行的解决方案之前就找到了自己问题的解决方案时,这真是太糟糕了……

尽管如此,我还是解决了它!

ModuleList 不需要在此处创建,而是在 .Click 事件处理程序中创建。它也没有被延续到其余的程序中。在使用它的地方

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

我的疏忽很糟糕。

It's so bad when you figure out the solution to your own problem before anybody can even guess at a viable fix...

Nevertheless I fixed it!

ModuleList did not need to be created here, but instead in a .Click event handler. It also wasn't being carried over the rest of the procedures. where it was being used

I just declared it globally used it from there.

Bad bad oversight on my part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文