ASP.NET、VB:检查选择了 CheckBoxList 中的哪些项目

发布于 2024-10-10 14:01:42 字数 456 浏览 7 评论 0原文

我知道这是一个非常基本的问题,但我找不到如何在 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

捂风挽笑 2024-10-17 14:01:42

您可以循环遍历 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

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

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.

星星的軌跡 2024-10-17 14:01:42

假设您的 aspx 看起来与此类似:

    <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>

您可以使用 ListItem's-Selected 属性来检查是否应启用您的文本框:

  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

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
感性不性感 2024-10-17 14:01:42

我不会这样做,效率很低。您只是为了启用或禁用文本框而访问服务器,您应该使用 javascript。下面的代码会更好

 <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>

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>
绝對不後悔。 2024-10-17 14:01:42

将它们放入字符串中的函数

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文