开发者

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

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 b开发者_开发知识库ehind, 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


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.


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


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>


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜