ChekBoxList 未注意到已选择,并且未显示值

ChekBoxList Not Noticing Selected, and not displaying value

未拾取已选中的内容,也未显示值。 当我调试代码并介入时:

VB 代码:

  1. 当调试 "li" 后每个都持有一个值(ex286)
  2. 当我转到项目并打开盒子时,我得到了这个:

项目 = 未为 'Public ReadOnly Default Property Item(index As Integer) As System.Web.UI.WebControls.ListItem' 的参数 'index' 指定参数。 Item = 为了计算一个索引 属性,属性 必须被限定并且参数必须由用户显式提供。

  1. If后的"li"有一个值(ex286),但是Selected是"FALSE"不知道为什么。
  2. =li之后是文本和Value(286)
  3. 另一件事,如果我单击它们,它只会给我第一个框的值而不是其余的值。

      Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
    
    For Each li As ListItem In CheckBoxList.Items
        If li.Selected Then
            Texttext.Text = li.Value
        Else                 
            Texttext.Text = "Give Up Loser!"
        End If
    NextEnd Sub
    

ASCX 文件

<asp:CheckBox ID="CheckBoxSelectAll" runat="server" Text="Select All" AutoPostBack="True" />
<asp:CheckBoxList ID="CheckBoxList" runat="server"
  DataSourceID="ObjectDataSource1" DataTextField="Name" DataValueField="Id"
  RepeatColumns="3"  ></asp:CheckBoxList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetStuff" 
DataObjectTypeName ="DataTransfer.TheData"
TypeName="BusinessDelegate.DataBusinessDelegate">
 </asp:ObjectDataSource>
 <asp:LinkButton ID="LinkButton" runat="server" Text="Here"></asp:LinkButton> 
<asp:Label ID ="Texttext" runat="server" Text=""></asp:Label>

我尝试了一些在线项目,但没有任何效果。

Get all selected values of CheckBoxList in VB.NET

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

我不确定你想用这个实现什么。 以下代码可能适合您。


Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
    Dim cbChecked As Boolean
    For lItem = 0 To CheckBoxList.Items.Count - 1
        cbChecked = CheckBoxList.GetItemChecked(lItem)
        If cbChecked Then
            Texttext.Text = CheckBoxList.GetItemText(lItem)
        Else
            Texttext.Text = "Give Up Loser!"
        End If
    Next
End Sub
</pre>

当你运行上面的代码时,如果最后一个复选框没有被选中那么你最终会得到 'Give Up Loser!' 在 'Texttext' 文本框中。

您可以使用以下代码获取列表中选中框的数量


Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
    Dim checkedBoxes = CheckBoxList.CheckedItems
    Dim checkedBoxesCount = checkedBoxes.Count
    For Each lItems In checkedBoxes
        Dim chkdCheckBoxName = lItems.ToString
    Next
End Sub
</pre>

试试下面的代码,在文本框中写入选中框的值。


Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
    Texttext.Text = "" 'clearing the text box
    Dim checkedBoxes = CheckBoxList.CheckedItems
    Dim checkedBoxesCount = checkedBoxes.Count
    For Each lItems In checkedBoxes
        Dim chkdCheckBoxName = lItems.ToString
        Texttext.Text = Texttext.Text & " | " & chkdCheckBoxName
    Next
    If checkedBoxesCount = 0 Then
        Texttext.Text = "Give Up Loser!"
    End If
End Sub

Above code is based on System.Windows.Forms.CheckedListBox