ASP.NET,VB:检查选中CheckBoxList的哪些项

Sar*_*ara 2 vb.net asp.net selecteditem listitem checkboxlist

我知道这是一个非常基本的问题,但我无法在VB中找到如何做到这一点...我有一个CheckBoxList,其中一个选项包含一个文本框来填写你自己的值.因此,当我的复选框(CheckBoxList中的ListItem)被选中时,我需要启用该文本​​框.这是后面的代码,我不知道在我的If语句中放入什么来测试是否检查了某个ListItem.

Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
    If ___ Then
        txtelect.Enabled = True
    Else
        txtelect.Enabled = False
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

Sco*_*ell 8

您可以遍历CheckBoxList中的复选框,检查每个复选框以查看是否已选中.尝试这样的事情:

For Each li As ListItem In CheckBoxList1.Items
    If li.Value = "ValueOfInterest" Then
        'Ok, this is the CheckBox we care about to determine if the TextBox should be enabled... is the CheckBox checked?
        If li.Selected Then
            'Yes, it is! Enable TextBox
            MyTextBox.Enabled = True
        Else
            'It is not checked, disable TextBox
            MyTextBox.Enabled = False
        End If
    End If
Next
Run Code Online (Sandbox Code Playgroud)

上面的代码将放在CheckBoxList的SelectedIndexChanged事件处理程序中.