How to retrieve multiple selected values from CheckBoxList in asp.net

asp.net checkboxlist multiple selected values
this is a simple tip to get checkboxlist multiple selected items values. checkboxlist has an event OnSelectedIndexChanged. when someone select an item from checkboxlist then the event trigger and we process some code in this time. we perform a linq query to get which items are selected. then we do a foreach loop to output checkboxlist selected items text and values in label. all coding are asp.net built in feature.
checkboxlist-multiple-selected-values.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>    
      
<!DOCTYPE html>
        
<script runat="server">
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var items = from ListItem li in CheckBoxList1.Items
                    where li.Selected == true
                    select li;
        
        Label1.Text = "you checked item(s).....<br />";
        
        foreach(ListItem li in items)
        {
            Label1.Text += li.Text + " | " + li.Value + "<br />";
        }
    }
</script>        
        
<html xmlns="http://www.w3.org/1999/xhtml">        
<head id="Head1" runat="server">        
    <title>asp.net checkboxlist multiple selected values</title>
</head>        
<body>        
    <form id="form1" runat="server">        
    <div>        
        <h2 style="color:MidnightBlue; font-style:italic;">        
            asp.net example - checkboxlist multiple selected values
        </h2>        
        <hr width="550" align="left" color="Gainsboro" />        
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Text="check item(s) from checkboxlist."
            Font-Size="X-Large"
            Width="350"
            >      
        </asp:Label>      
        <br /><br />
        <asp:CheckBoxList 
            ID="CheckBoxList1"
            runat="server"
            RepeatColumns="2"
            AutoPostBack="true"
            OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"
            >
            <asp:ListItem Text="Rook" Value="1"></asp:ListItem>
            <asp:ListItem Text="American Crow" Value="2"></asp:ListItem>
            <asp:ListItem Text="White-necked Raven" Value="3"></asp:ListItem>
            <asp:ListItem Text="Carrion Crow" Value="4"></asp:ListItem>
            <asp:ListItem Text="Northern Raven" Value="5"></asp:ListItem>
        </asp:CheckBoxList>  
    </div>        
    </form>        
</body>        
</html>
More asp.net examples