locked
how do i know which item has been Clicked instead of Selected. (CheckBoxList) RRS feed

  • Question

  • User944339287 posted

    hi guys... The following is my HTML code.

    how can I know which item has been clicked in CodeBehind?
    * Notes: I just wanna know which item has been Clicked instead of Selected.

    For example, the system will show [Banana] first if [Banana] is clicked instead of [Mango]

    <asp:CheckBoxList ID="chkFruits" runat="server">
        <asp:ListItem Text="Mango" Value="1" Selected="true"></asp:ListItem>
        <asp:ListItem Text="Orange" Value="2"></asp:ListItem>
        <asp:ListItem Text="Banana" Value="3"></asp:ListItem>
        <asp:ListItem Text="Apple" Value="4"></asp:ListItem>
        <asp:ListItem Text="Papaya" Value="5"></asp:ListItem>
    </asp:CheckBoxList>



    Thursday, March 4, 2021 10:46 AM

All replies

  • User-943250815 posted

    Check accepted answer on this thread
    https://forums.asp.net/t/2091095.aspx?checkboxlist+selectedindexcahnged+event

    Thursday, March 4, 2021 12:11 PM
  • User-189459990 posted

    What is the type of your project? Is it a ASP.NET written in VB.NET? 

    If so, you can create a variable to store the selected items, and then remove and re-insert the last item.

    .aspx

    <asp:CheckBoxList ID="chkFruits" runat="server" OnSelectedIndexChanged="chkFruits_SelectedIndexChanged" AutoPostBack="true">
        ...     
    </asp:CheckBoxList>

    .vb

    Private items As List(Of ListItem)
    
    Protected Sub chkFruits_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        If chkFruits.SelectedItem IsNot Nothing Then
            Items = chkFruits.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
            Dim item As ListItem = Items.LastOrDefault()
            chkFruits.Items.Remove(item)
            chkFruits.Items.Insert(0, item)
        End If
    End Sub

    Friday, March 5, 2021 6:52 AM