前面发了文本框和下拉列表框的,这回发一个CheckBoxList。不知道中文名字该叫什么。
CheckBoxList 最郁闷的地方就是:明明可以选择多个选项,但是 SelectedValue 只能得到第一个选项,其他被选中的还得另想办法。
不知道vs2005 有没有改进,至少 vs2003 是这样的。
Public Function GetSelectedValue()Function GetSelectedValue() As String
Dim item As ListItem
Dim tmpStr As String = ""
For Each item In Me.Items
If item.Selected Then
tmpStr &= item.Value & ","
End If
Next
tmpStr = tmpStr.TrimEnd(",")
Return tmpStr
End Function
于是呢加了这个函数GetSelectedValue 来获取所有的选中的项的Value 值,用 “,”来分隔。
对应的还有
GetSelectedText //获取 所有的选中的项的Text 值
SetSelectedByValue //通过 Value 设置 选项
SetSelectedByText //通过 Text 设置 选项
Public Sub SetSelectedByValue()Sub SetSelectedByValue(ByVal listValues As String)
Me.SelectedIndex = -1
If (listValues = "True") Then
listValues = "1"
End If
If (listValues = "False") Then
listValues = "0"
End If
Dim item As ListItem
Dim i As Int32 = 0
Dim tmpStr() As String = listValues.Split(",")
For Each item In Me.Items
For i = 0 To tmpStr.Length - 1
If (item.Value.Equals(tmpStr(i))) Then
item.Selected = True
End If
Next
Next
End Sub
其他的地方就和 下拉列表框基本一致了,可以看看 第二步:下拉列表框
我就不重复写了,感兴趣的话看看下面的代码。
Imports System.ComponentModel
Imports System.Web.UI
Imports HBS
Imports System
Imports System.Web.UI.WebControls
Imports System.Data
<ToolboxData("")> _
Public Class HBSCheckBoxListClass HBSCheckBoxList
Inherits System.Web.UI.WebControls.CheckBoxList
Implements IGetControlValue
实现接口#Region "实现接口"
<Bindable(True), Category("默认值"), DefaultValue("210"), Description("获取控件类别")> _
ReadOnly Property ControlKind()Property ControlKind() As String Implements IGetControlValue.ControlKind
Get
Return "210"
End Get
End Property
Public Function GetValue()Function GetValue() As String Implements IGetControlValue.GetControlValue
Return Me.GetSelectedValue
End Function
Public Function GetValue()Function GetValue(ByVal kind As String) As String Implements IGetControlValue.GetControlValue
Return ""
End Function
Public Sub SetValue()Sub SetValue(ByVal value As String) Implements IGetControlValue.SetControlValue
Me.SetSelectedByValue(value)
End Sub
Public Sub SetValue()Sub SetValue(ByVal value As String, ByVal kind As String) Implements IGetControlValue.SetControlValue
End Sub
#End Region
初始化 OnInit。设置CssClass、DataValueField、DataTextField、#Region "初始化 OnInit。设置CssClass、DataValueField、DataTextField、"
Protected Overrides Sub OnInit()Sub OnInit(ByVal e As EventArgs)
Me.CssClass = "chk"
Me.DataValueField = "ID"
Me.DataTextField = "txt"
Me.Font.Size = FontUnit.Point(9)
End Sub
#End Region
'根据传入的ID设置下拉列表框的默认选项,如果没有找到,不选择,不抛出异常。
'ID值,多个话用 | 分隔
函数实现 setSelectedByValue#Region "函数实现 setSelectedByValue"
Public Sub SetSelectedByValue()Sub SetSelectedByValue(ByVal listValues As String)
Me.SelectedIndex = -1
If (listValues = "True") Then
listValues = "1"
End If
If (listValues = "False") Then
listValues = "0"
End If
Dim item As ListItem
Dim i As Int32 = 0
Dim tmpStr() As String = listValues.Split(",")
For Each item In Me.Items
For i = 0 To tmpStr.Length - 1
If (item.Value.Equals(tmpStr(i))) Then
item.Selected = True
End If
Next
Next
End Sub
#End Region
' 根据传入的文本内容设置下拉列表框的默认选项,如果没有找到,选第一项,不抛出异常。
' 文本内容
函数实现 SetSelectedByText#Region "函数实现 SetSelectedByText"
Public Sub SetSelectedByText()Sub SetSelectedByText(ByVal listTexts As String)
Me.SelectedIndex = -1
Dim item As ListItem
Dim tmpStr() As String = listTexts.Split(",")
Dim i As Int32 = 0
For Each item In Me.Items
For i = 0 To tmpStr.Length - 1
If item.Text.Equals(tmpStr(i)) Then
item.Selected = True
End If
Next
Next
End Sub
#End Region
'返回选择的ID值,用 , 分隔
函数实现 GetSelectedValue#Region "函数实现 GetSelectedValue"
Public Function GetSelectedValue()Function GetSelectedValue() As String
Dim item As ListItem
Dim tmpStr As String = ""
For Each item In Me.Items
If item.Selected Then
tmpStr &= item.Value & ","
End If
Next
tmpStr = tmpStr.TrimEnd(",")
Return tmpStr
End Function
#End Region
'返回选择的Text值,用 , 分隔
函数实现 GetSelectedText#Region "函数实现 GetSelectedText"
Public Function GetSelectedText()Function GetSelectedText() As String
Dim item As ListItem
Dim tmpStr As String = ""
For Each item In Me.Items
If item.Selected Then
tmpStr &= item.Text & ","
End If
Next
tmpStr = tmpStr.TrimEnd(",")
Return tmpStr
End Function
#End Region
' 传入查询语句,绑定下拉列表框。正确执行返回空字符串,不正确返回错误信息
' 查询语句
'
函数实现 bindList#Region "函数实现 bindList"
Public Function BindListBySQL()Function BindListBySQL(ByVal sqlString As String)
Dim dal = New DataAccessLayer
Me.DataSource = dal.RunSqlDataTable(sqlString)
Me.DataBind()
dal.Dispose()
End Function
#End Region
函数实现 bindList#Region "函数实现 bindList"
Public Function BindListBySQL()Function BindListBySQL(ByVal sqlString As String, ByVal isAddItem As Boolean)
Dim dal = New DataAccessLayer
Me.DataSource = dal.RunSqlDataTable(sqlString)
Me.DataBind()
dal.Dispose()
End Function
#End Region
' 传入查询语句,绑定下拉列表框。正确执行返回空字符串,不正确返回错误信息
' 查询语句
'
函数实现 bindList#Region "函数实现 bindList"
Public Function BindListByStore()Function BindListByStore(ByVal store As String) As String
Dim dal As New DataAccessLayer
Me.DataSource = dal.RunStoreDataTable(store)
Me.DataBind()
dal.Dispose()
End Function
#End Region
' 添加日期。给下拉列表框填充从 1 到 lastDay 的数据。value 和 text 值一致。
' 最后一天
' 是否添加 “请选择”
函数实现 ItemAddDate#Region "函数实现 ItemAddDate"
Public Sub ItemAddDate()Sub ItemAddDate(ByVal lastDay As Int32)
Dim i As Int32
For i = 1 To lastDay
Me.Items.Add(New ListItem(i.ToString(), i.ToString()))
Next
End Sub
#End Region
' 添加月份。给下拉列表框填充从 1 到 12 的数据。value 和 text 值一致。
'
函数实现 ItemAddMonth#Region "函数实现 ItemAddMonth"
Public Sub ItemAddMonth()Sub ItemAddMonth()
Dim i As Int32
For i = 1 To 12
Me.Items.Add(New ListItem(i.ToString(), i.ToString()))
Next
End Sub
#End Region
' 用两个字符串来添加选项。
' 显示的内容,用的字符串,用“~”分开。
' value值,用的字符串,用“~”分开。
函数实现 AddItemByString#Region "函数实现 AddItemByString"
Public Sub AddItemByString()Sub AddItemByString(ByVal values As String, ByVal texts As String)
Dim strText As String() = texts.Split("~")
Dim strValue As String() = values.Split("~")
Dim i As Int32
For i = 1 To strText.Length
Me.Items.Add(New ListItem(strText(i), strValue(i)))
Next
End Sub
#End Region
' 用一个字符串来添加选项。value在前,Text在后
' 显示的内容,用的字符串,用“~”分开。
函数实现 AddItemByString#Region "函数实现 AddItemByString"
Public Sub AddItemByString()Sub AddItemByString(ByVal TextsAndValues As String)
Dim str As String() = TextsAndValues.Split("~")
Dim Len As Int32 = str.Length \ 2
Dim i As Int32
For i = 0 To Len - 1
Me.Items.Add(New ListItem(str(Len + i), str(i)))
Next
End Sub
#End Region
' 用两个数组来添加选项。
' 显示的内容。
' value值。
函数实现 AddItemByArray#Region "函数实现 AddItemByArray"
Public Sub AddItemByArray()Sub AddItemByArray(ByVal values As String(), ByVal texts As String())
Dim i As Int32
For i = 0 To texts.Length
Me.Items.Add(New ListItem(texts(i), values(i)))
Next
End Sub
#End Region
' 用一个数组来添加选项。values在前,Text在后
' 显示的内容,用的字符串,用“~”分开。
'
函数实现 AddItemByArray#Region "函数实现 AddItemByArray"
Public Sub AddItemByArray()Sub AddItemByArray(ByVal ValuesAndTexts As String())
Dim len As Int32 = ValuesAndTexts.Length / 2
Dim i As Int32
For i = 0 To len
Me.Items.Add(New ListItem(ValuesAndTexts(len + i), ValuesAndTexts(i)))
Next
End Sub
#End Region
函数实现 AddItemByArrayTwo#Region "函数实现 AddItemByArrayTwo"
Public Sub AddItemByArrayTwo()Sub AddItemByArrayTwo(ByVal ValuesAndTexts As String(,))
Dim len As Int32 = ValuesAndTexts.Length / 2 - 1
Dim i As Int32
For i = 0 To len
Me.Items.Add(New ListItem(ValuesAndTexts(i, 1), ValuesAndTexts(i, 0)))
Next
End Sub
#End Region
End Class