【发布时间】: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