摘要
承上篇「扩展 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 值(以逗号分隔)。
1
Imports System.Web.UI.WebControls
2
Imports System.ComponentModel
3
Imports System.Text
4
5
Namespace WebControls
6
Public Class TBCheckBoxList
7
Inherits CheckBoxList
8
9
''' <summary>
10
''' 项目复选的 Value 值,以逗号分隔每个项目。
11
''' </summary>
12
< _
13
Description("项目复选的 Value 值,以逗号分隔每个项目。"), _
14
Bindable(True), _
15
Browsable(False) _
16
> _
17
Public Property SelectedValueList() As String
18
Get
19
Return GetSelectedValueList()
20
End Get
21
Set(ByVal value As String)
22
SetSelectedValueList(value)
23
End Set
24
End Property
25
26
''' <summary>
27
''' 取得项目复选的 Value 值。
28
''' </summary>
29
Private Function GetSelectedValueList() As String
30
Dim oValues As StringBuilder
31
Dim oItem As ListItem
32
33
oValues = New StringBuilder()
34
For Each oItem In Me.Items
35
If oItem.Selected Then
36
If oValues.Length > 0 Then oValues.Append(",")
37
oValues.Append(oItem.Value)
38
End If
39
Next
40
Return oValues.ToString()
41
End Function
42
43
''' <summary>
44
''' 设定项目复选的 Value 值。
45
''' </summary>
46
''' <param name="Value">复选 Value 值。</param>
47
Private Sub SetSelectedValueList(ByVal Value As String)
48
Dim oValues() As String
49
Dim sValue As String
50
Dim oItem As ListItem
51
52
Me.ClearSelection()
53
oValues = Split(Value, ",")
54
For Each sValue In oValues
55
oItem = Me.Items.FindByValue(sValue)
56
If oItem IsNot Nothing Then
57
oItem.Selected = True
58
End If
59
Next
60
End Sub
61
62
''' <summary>
63
''' 项目复选的 Text 值,以逗号分隔每个项目。
64
''' </summary>
65
< _
66
Description("项目复选的 Text 值,以逗号分隔每个项目。"), _
67
Bindable(True), _
68
Browsable(False) _
69
> _
70
Public Property SelectedTextList() As String
71
Get
72
Return GetSelectedTextList()
73
End Get
74
Set(ByVal value As String)
75
SetSelectedTextList(value)
76
End Set
77
End Property
78
79
''' <summary>
80
''' 取得项目复选的 Text 值。
81
''' </summary>
82
Private Function GetSelectedTextList() As String
83
Dim oValues As StringBuilder
84
Dim oItem As ListItem
85
86
oValues = New StringBuilder()
87
For Each oItem In Me.Items
88
If oItem.Selected Then
89
If oValues.Length > 0 Then oValues.Append(",")
90
oValues.Append(oItem.Text)
91
End If
92
Next
93
Return oValues.ToString()
94
End Function
95
96
''' <summary>
97
''' 设定项目复选的 Text 值。
98
''' </summary>
99
''' <param name="Value">复选 Text 值。</param>
100
Private Sub SetSelectedTextList(ByVal Value As String)
101
Dim oValues() As String
102
Dim sValue As String
103
Dim oItem As ListItem
104
105
Me.ClearSelection()
106
oValues = Split(Value, ",")
107
For Each sValue In oValues
108
oItem = Me.Items.FindByText(sValue)
109
If oItem IsNot Nothing Then
110
oItem.Selected = True
111
End If
112
Next
113
End Sub
114
115
End Class
116
End Namespace
Imports System.Web.UI.WebControls2
Imports System.ComponentModel3
Imports System.Text4

5
Namespace WebControls6
Public Class TBCheckBoxList7
Inherits CheckBoxList8

9
''' <summary>10
''' 项目复选的 Value 值,以逗号分隔每个项目。11
''' </summary>12
< _13
Description("项目复选的 Value 值,以逗号分隔每个项目。"), _14
Bindable(True), _15
Browsable(False) _16
> _17
Public Property SelectedValueList() As String18
Get19
Return GetSelectedValueList()20
End Get21
Set(ByVal value As String)22
SetSelectedValueList(value)23
End Set24
End Property25

26
''' <summary>27
''' 取得项目复选的 Value 值。28
''' </summary>29
Private Function GetSelectedValueList() As String30
Dim oValues As StringBuilder31
Dim oItem As ListItem32

33
oValues = New StringBuilder()34
For Each oItem In Me.Items35
If oItem.Selected Then36
If oValues.Length > 0 Then oValues.Append(",")37
oValues.Append(oItem.Value)38
End If39
Next40
Return oValues.ToString()41
End Function42

43
''' <summary>44
''' 设定项目复选的 Value 值。45
''' </summary>46
''' <param name="Value">复选 Value 值。</param>47
Private Sub SetSelectedValueList(ByVal Value As String)48
Dim oValues() As String49
Dim sValue As String50
Dim oItem As ListItem51

52
Me.ClearSelection()53
oValues = Split(Value, ",")54
For Each sValue In oValues55
oItem = Me.Items.FindByValue(sValue)56
If oItem IsNot Nothing Then57
oItem.Selected = True58
End If59
Next60
End Sub61

62
''' <summary>63
''' 项目复选的 Text 值,以逗号分隔每个项目。64
''' </summary>65
< _66
Description("项目复选的 Text 值,以逗号分隔每个项目。"), _67
Bindable(True), _68
Browsable(False) _69
> _70
Public Property SelectedTextList() As String71
Get72
Return GetSelectedTextList()73
End Get74
Set(ByVal value As String)75
SetSelectedTextList(value)76
End Set77
End Property78

79
''' <summary>80
''' 取得项目复选的 Text 值。81
''' </summary>82
Private Function GetSelectedTextList() As String83
Dim oValues As StringBuilder84
Dim oItem As ListItem85

86
oValues = New StringBuilder()87
For Each oItem In Me.Items88
If oItem.Selected Then89
If oValues.Length > 0 Then oValues.Append(",")90
oValues.Append(oItem.Text)91
End If92
Next93
Return oValues.ToString()94
End Function95

96
''' <summary>97
''' 设定项目复选的 Text 值。98
''' </summary>99
''' <param name="Value">复选 Text 值。</param>100
Private Sub SetSelectedTextList(ByVal Value As String)101
Dim oValues() As String102
Dim sValue As String103
Dim oItem As ListItem104

105
Me.ClearSelection()106
oValues = Split(Value, ",")107
For Each sValue In oValues108
oItem = Me.Items.FindByText(sValue)109
If oItem IsNot Nothing Then110
oItem.Selected = True111
End If112
Next113
End Sub114

115
End Class116
End Namespace测试程序
在页面上放置 TBCheckBoxList 控件,我们只要使用 SelectedValueList 或 SelectedTextList 属性值,就可以轻易的取得或设定复选值。
![]()
![]()
1
Protected Sub btnGetSelectedValueList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetSelectedValueList.Click
2
txtValue.Text = CheckBoxList1.SelectedValueList
3
End Sub
4
5
Protected Sub btnSetSelectedValueList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSetSelectedValueList.Click
6
CheckBoxList1.SelectedValueList = txtValue.Text
7
End Sub
8
9
10
Protected Sub btnGetSelectedTextList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetSelectedTextList.Click
11
txtValue.Text = CheckBoxList1.SelectedTextList
12
End Sub
13
14
Protected Sub btnSetSelectedTextList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSetSelectedTextList.Click
15
CheckBoxList1.SelectedTextList = txtValue.Text
16
End Sub
Protected Sub btnGetSelectedValueList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetSelectedValueList.Click2
txtValue.Text = CheckBoxList1.SelectedValueList3
End Sub4

5
Protected Sub btnSetSelectedValueList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSetSelectedValueList.Click6
CheckBoxList1.SelectedValueList = txtValue.Text7
End Sub8

9

10
Protected Sub btnGetSelectedTextList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetSelectedTextList.Click11
txtValue.Text = CheckBoxList1.SelectedTextList12
End Sub13

14
Protected Sub btnSetSelectedTextList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSetSelectedTextList.Click15
CheckBoxList1.SelectedTextList = txtValue.Text16
End Sub如果 TBCheckBoxList 的 SelectedValueList 及 SelectedTextList 属性需要系结字段时,只需直接设定即可。
![]()

浙公网安备 33010602011771号