ASP.NET: Get a list of all selected items in a CheckBoxList
From Wiki
Summary: Get a list of each item that was checked in a CheckBoxList
An example page with a CheckBoxList:
- <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:CheckBoxList ID="CheckBoxList1" runat="server">
- <asp:ListItem Text="1" Value="1"></asp:ListItem>
- <asp:ListItem Text="2" Value="2"></asp:ListItem>
- <asp:ListItem Text="3" Value="3"></asp:ListItem>
- </asp:CheckBoxList>
- <asp:Button ID="Button1" runat="server" Text="Button" />
- </div>
- </form>
- </body>
- </html>
How to retrieve each selected value:
- Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
- For Each li As ListItem In CheckBoxList1.Items
- If li.Selected = True Then Response.Write(li.Value & "<br>")
- Next
- End Sub
This Hack is part of the ASP.NET Hacks collection


