Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

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: 3.0/5 (4 votes cast)