开发者

How do I keep Selected ListItem from ignoring other ListItems?

开发者 https://www.devze.com 2023-03-28 05:31 出处:网络
When I run code the below, the If statement never resolves to \'True\'. It always shows \'Assm\' as the SelectedItem, even if I check all the checkboxes.

When I run code the below, the If statement never resolves to 'True'. It always shows 'Assm' as the SelectedItem, even if I check all the checkboxes.

So how do I allow 'Assm' to be checked by default AND have the code-behind see that the other checkboxes are checked?

<asp:CheckBoxList ID="qualityChecks" runat="server" Repe开发者_如何学PythonatDirection="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


SelectedItem of a CheckBoxList works that way.
What you want to do is iterate through the ListItems and see if they are Checked.

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


Try creating an empty CheckListBox:

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

and than adding the ListItems on Page_Load

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


Try:

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

As you have specified no value for the list items, they take the value of the Text

0

精彩评论

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