我的复选框列表有问题,只能选择一项而不是选择多项。有人可以帮我编写代码吗?
<asp:CheckBoxList class="list-group-item" ID="chkResourceName1" runat="server" AutoPostBack ="true" OnSelectedIndexChanged ="ResourceName_Click"></asp:CheckBoxList>
Sub ResourceName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Dim a As Integer = chkResourceName1.Items.Count
Dim count As Integer = 0
For i As Integer = 0 To a - 1
If chkResourceName1.Items(i).Selected = True Then count += 1
Next
If count > 1 Then
For i As Integer = 0 To a - 1
If chkResourceName1.Items(i).Selected = True Then
chkResourceName1.Items(i).Selected = False
End If
Next
End If
Catch ex As Exception
attPage.ErrorMessage = DA.GetErrorMessage(1, System.Reflection.MethodBase.GetCurrentMethod.Name.ToString, ErrMsg, ex.Message.ToString, attPage.ActionPage)
ShowError(attPage.ErrorHeader, attPage.ErrorMessage)
End Try
End Sub
此代码结果是当用户检查了一个以上,然后两个检查均被清除时。我要确定,如果用户单击多个,它将清除上一个,而仅检查最后一个。
答案 0 :(得分:1)
尝试一下,注释得到注释
Sub ResourceName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Dim a As Integer = chkResourceName1.Items.Count
Dim s As Integer = chkResourceName1.SelectedIndex
'Store the currently selected item's index in a variable
For i As Integer = 0 To a - 1
chkResourceName1.Items(i).Selected = False
' Un check all items
Next
chkResourceName1.Items(s).Selected = True
'check the current item
Catch ex As Exception
attPage.ErrorMessage = DA.GetErrorMessage(1, System.Reflection.MethodBase.GetCurrentMethod.Name.ToString, ErrMsg, ex.Message.ToString, attPage.ActionPage)
ShowError(attPage.ErrorHeader, attPage.ErrorMessage)
End Try