CheckBoxList Class - ASP.NET

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.




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