如何使Selected ListItem不会忽略其他ListItem?

时间:2011-08-16 13:34:08

标签: asp.net vb.net visual-studio-2008 asp.net-3.5

当我运行下面的代码时,If语句永远不会解析为'True'。它总是将'Assm'显示为SelectedItem,即使我选中了所有复选框。

那么我如何允许默认选中“Assm”并让代码隐藏看到其他复选框被选中?

<asp:CheckBoxList ID="qualityChecks" runat="server" RepeatDirection="Horizontal" TabIndex="8">
    <asp:ListItem Text="Assm" Selected="True"></asp:ListItem>
    <asp:ListItem Text="Qual"></asp:ListItem>
    <asp:ListItem Text="PMgr"></asp:ListItem>
    <asp:ListItem Text="Plant"></asp:ListItem>
</asp:CheckBoxList>

If qualityChecks.SelectedItem.Text = "Qual" Then
    'Some Code
End If

3 个答案:

答案 0 :(得分:1)

CheckBoxList的SelectedItem以这种方式工作 你想要做的是遍历ListItems并查看它们是否被检查。

For each li as ListItem in qualitychecks.items
  if li.checked and li.text = "Qual" then       
    'some code
  end if
next

答案 1 :(得分:0)

尝试创建一个空的CheckListBox:

    <asp:CheckBoxList ID="qualityChecks" runat="server" RepeatDirection="Horizontal" TabIndex="8">
    </asp:CheckBoxList>

并在Page_Load

上添加ListItems
Protected Sub Page_Load(sender As Object, e As EventArgs)

    Dim l1 As New ListItem()
    Dim l2 As New ListItem()
    Dim l3 As New ListItem()
    Dim l4 As New ListItem()
    l1.Text = "Assm"
    l2.Text = "Qual"
    l3.Text = "PMgr"
    l4.Text = "Plant"
    If Not Page.IsPostBack Then
        l1.Selected = True
        qualityChecks.Items.Add(l1)
        qualityChecks.Items.Add(l2)
        qualityChecks.Items.Add(l3)
        qualityChecks.Items.Add(l4)
    End If
End Sub

答案 2 :(得分:0)

尝试:

If qualityChecks.SelectedValue = "Qual" Then
    'Some Code
End If

由于您没有为列表项指定任何值,因此它们采用Text

的值