ASP.NET, VB: checking which items of a CheckBoxList are selected

≯℡__Kan透↙ 提交于 2019-12-10 22:58:12

问题


I know this is an extremely basic question, but I couldn't find how to do this in VB... I have a CheckBoxList where one of the options includes a textbox to fill in your own value. So I need to have that textbox become enabled when its checkbox (a ListItem in the CheckBoxList) is checked. This is the code behind, I'm not sure what to put in my If statement to test if that certain ListItem is checked.

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

回答1:


You can loop through the checkboxes in a CheckBoxList, checking each to see if it is checked. Try something like this:

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

The above code would be placed in the CheckBoxList's SelectedIndexChanged event handler.




回答2:


Assuming that your aspx look similar to this:

    <asp:TextBox ID="txtelect" runat="server"></asp:TextBox>
    <asp:CheckBoxList id="CheckBoxList1" runat="server" autopostback="true" >
        <asp:ListItem  Text="enable TextBox" Value="0" Selected="True"></asp:ListItem>
        <asp:ListItem  Text="1" Value="1" ></asp:ListItem>
        <asp:ListItem  Text="2" Value="2" ></asp:ListItem>
        <asp:ListItem  Text="3" Value="3" ></asp:ListItem>
    </asp:CheckBoxList>

you can use the ListItem's-Selected property to check if your Textbox should be enabled:

  Private Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
        'use the index of the ListItem where the user can enable the TextBox(starts with 0)'
         txtelect.Enabled = CheckBoxList1.Items( 0 ).Selected
  End Sub



回答3:


I wouldn't do it this way, it's very inefficient. You are hitting the server just to enable or disable a text box, you should use javascript. The code below would be better

 <asp:DataList ID="mylist" runat="server">
        <ItemTemplate>
            <input type="checkbox" id="chk<%#Container.ItemIndex %>" onclick="document.getElementById('txt<%#Container.ItemIndex %>').disabled=(!this.checked);" />
            <input type="text" id="txt<%#Container.ItemIndex %>" disabled="disabled" />
        </ItemTemplate>
    </asp:DataList>



回答4:


Funtion to get them in a string

Function ValueSelected(cbl As CheckBoxList, separator As String) As String
    Dim s As String = ""
    Dim cb As ListItem
    For Each cb In cbl.Items
        If cb.Selected Then
            s += cb.Text & separator
        end If
    Next
    s = s.Substring(0, s.Length - 1)
    Return s
End Function


来源:https://stackoverflow.com/questions/4629571/asp-net-vb-checking-which-items-of-a-checkboxlist-are-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!