Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Get a list of all selected items in a CheckBoxList

From Wiki

Jump to: navigation, search

Summary: Get a list of each item that was checked in a CheckBoxList

An example page with a CheckBoxList:

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" %>  
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title>Untitled Page</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:CheckBoxList ID="CheckBoxList1" runat="server">  
  13.         <asp:ListItem Text="1" Value="1"></asp:ListItem>  
  14.         <asp:ListItem Text="2" Value="2"></asp:ListItem>  
  15.         <asp:ListItem Text="3" Value="3"></asp:ListItem>  
  16.         </asp:CheckBoxList>  
  17.         <asp:Button ID="Button1" runat="server" Text="Button" />  
  18.     </div>  
  19.     </form>  
  20. </body>  
  21. </html>

How to retrieve each selected value:

  1. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.     For Each li As ListItem In CheckBoxList1.Items
  3.         If li.Selected = True Then Response.Write(li.Value & "<br>")
  4.     Next
  5. End Sub


This Hack is part of the ASP.NET Hacks collection

438 Rating: 0.0/5 (0 votes cast)