Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

CheckBoxList Class - ASP.NET

RSS
Modified on Wed, Dec 02, 2009, 2:53 PM by Administrator Categorized as ASP·NET Web Forms, Class Library
This page is part of the Class Library Pages collection.
Click the icon to see the index.

Overview

The following class enhances the standard ASP.NET CheckBoxList control with the following methods.

  • Bind() — Selects checkboxes according to the value of a specified field in a specified data table.

  • GetCheckCount() — Returns the number of checkboxes selected. Useful for validation purposes.

  • GetSelectedItemsXml() — Returns the values of the selected checkboxes in <x v='value'/>... format.

Source Code

VB.NET

{copytext|SourceVb}
Imports Microsoft.VisualBasic
Imports System.Data

Namespace AcmeBroomCompany
    Public Class CheckBoxList
    Inherits System.Web.UI.WebControls.CheckBoxList

        '------------------------------------------------------------------------------------------
        Public Sub Bind(ByVal source As DataTable, ByVal valueField As String)

            For Each row As DataRow In source.Rows

                Dim s As String = row(valueField).ToString()
                Dim found As Boolean = False

                For Each item As ListItem In Me.Items

                    If item.Value.ToString() = s Then
                        item.Selected = True
                        found = True
                        Exit For
                    End If

                Next

            Next

        End Sub
        '------------------------------------------------------------------------------------------
        Public Function GetCheckCount() As Integer

            Dim result As Integer = 0

            For Each item As ListItem In Me.Items
                If item.Selected Then
                    result += 1
                End If
            Next

            Return result

        End Function
        '------------------------------------------------------------------------------------------
        Public Function GetSelectedItemsXml() As String

            Dim result As String = ""

            For Each item As ListItem In Me.Items
                If item.Selected Then
                    result &= "<x v='"
                    result &= item.Value.Replace("'", "''")
                    result &= "'/>"
                End If
            Next

            Return result

        End Function

    End Class

End Namespace

C#

{copytext|SourceCs}
//TODO

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.