擴展 CheckBoxList 控制項 - 繫結複選項目(2)

擴展 CheckBoxList 控制項 - 繫結複選項目(2)

摘要

承上篇「擴展 CheckBoxList 控制項 - 繫結複選項目」中以整數值來描述複選項目,本文將以另一種方式,利用 Item 的 Value 或 Text 屬性來描述複選項目,每個被勾選的項目,會將其 Value 或 Text 屬性值,以逗點分隔的方式被記錄下來。例如

選項一.Value = 1

選項二.Value = 2

選項三.Value = 3

選項四.Value = 4

當複選了「選項一」及「選項三」,則複選值為 "1,3"。當複選了「選項二」及「選項四」,則複選值為 "2,4"。若要使用上述的方式來描述複選項目,一般要自行撰寫程式碼來處理。本文將擴展 CheckBoxList 控制項,直接透過屬性來雙向繫結這個複選值。

擴展 CheckBoxList 控制項

繼承 CheckBoxList 命名為 TBCheckBoxList,新增 SelectedValueList屬性,來描述目複選的 Value 值(以逗號分隔),新增 SelectedTextList 屬性,來描述項目複選的 Text 值(以逗號分隔)。

 

Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.Text

Namespace WebControls
    Public Class TBCheckBoxList
        Inherits CheckBoxList

        ''' <summary>
        ''' 項目複選的 Value 值,以逗號分隔每個項目。
        ''' </summary>
        < _
        Description("項目複選的 Value 值,以逗號分隔每個項目。"), _
        Bindable(True), _
        Browsable(False) _
        > _
        Public Property SelectedValueList() As String
            Get
                Return GetSelectedValueList()
            End Get
            Set(ByVal value As String)
                SetSelectedValueList(value)
            End Set
        End Property

        ''' <summary>
        ''' 取得項目複選的 Value 值。
        ''' </summary>
        Private Function GetSelectedValueList() As String
            Dim oValues As StringBuilder
            Dim oItem As ListItem

            oValues = New StringBuilder()
            For Each oItem In Me.Items
                If oItem.Selected Then
                    If oValues.Length > 0 Then oValues.Append(",")
                    oValues.Append(oItem.Value)
                End If
            Next
            Return oValues.ToString()
        End Function

        ''' <summary>
        ''' 設定項目複選的 Value 值。
        ''' </summary>
        ''' <param name="Value">複選 Value 值。</param>
        Private Sub SetSelectedValueList(ByVal Value As String)
            Dim oValues() As String
            Dim sValue As String
            Dim oItem As ListItem

            Me.ClearSelection()
            oValues = Split(Value, ",")
            For Each sValue In oValues
                oItem = Me.Items.FindByValue(sValue)
                If oItem IsNot Nothing Then
                    oItem.Selected = True
                End If
            Next
        End Sub

        ''' <summary>
        ''' 項目複選的 Text 值,以逗號分隔每個項目。
        ''' </summary>
        < _
        Description("項目複選的 Text 值,以逗號分隔每個項目。"), _
        Bindable(True), _
        Browsable(False) _
        > _
        Public Property SelectedTextList() As String
            Get
                Return GetSelectedTextList()
            End Get
            Set(ByVal value As String)
                SetSelectedTextList(value)
            End Set
        End Property

        ''' <summary>
        ''' 取得項目複選的 Text 值。
        ''' </summary>
        Private Function GetSelectedTextList() As String
            Dim oValues As StringBuilder
            Dim oItem As ListItem

            oValues = New StringBuilder()
            For Each oItem In Me.Items
                If oItem.Selected Then
                    If oValues.Length > 0 Then oValues.Append(",")
                    oValues.Append(oItem.Text)
                End If
            Next
            Return oValues.ToString()
        End Function

        ''' <summary>
        ''' 設定項目複選的 Text 值。
        ''' </summary>
        ''' <param name="Value">複選 Text 值。</param>
        Private Sub SetSelectedTextList(ByVal Value As String)
            Dim oValues() As String
            Dim sValue As String
            Dim oItem As ListItem

            Me.ClearSelection()
            oValues = Split(Value, ",")
            For Each sValue In oValues
                oItem = Me.Items.FindByText(sValue)
                If oItem IsNot Nothing Then
                    oItem.Selected = True
                End If
            Next
        End Sub

    End Class
End Namespace

 

測試程式

在頁面上放置 TBCheckBoxList 控制項,我們只要使用 SelectedValueList 或 SelectedTextList 屬性值,就可以輕易的取得或設定複選值。

image

image

   Protected Sub btnGetSelectedValueList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetSelectedValueList.Click
        txtValue.Text = CheckBoxList1.SelectedValueList
    End Sub

    Protected Sub btnSetSelectedValueList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSetSelectedValueList.Click
        CheckBoxList1.SelectedValueList = txtValue.Text
    End Sub


    Protected Sub btnGetSelectedTextList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetSelectedTextList.Click
        txtValue.Text = CheckBoxList1.SelectedTextList
    End Sub

    Protected Sub btnSetSelectedTextList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSetSelectedTextList.Click
        CheckBoxList1.SelectedTextList = txtValue.Text
    End Sub

如果 TBCheckBoxList 的 SelectedValueList 及 SelectedTextList 屬性需要繫結欄位時,只需直接設定即可。

image

ASP.NET 魔法學院