001.
<%@ Import
Namespace
=
"System.Data"
%>
002.
<%@ Import
Namespace
=
"System.Data.OleDb"
%>
003.
<%@ Page Language=
"VB"
%>
004.
<script runat=
"server"
>
005.
006.
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
007.
IF
Not
Page.IsPostBack()
Then
008.
CheckBoxListDataTable()
009.
CheckBoxListDataTableRows()
010.
CheckBoxListSortedList()
011.
CheckBoxListAddInsertItem()
012.
End
IF
013.
End
Sub
014.
015.
016.
Function
CheckBoxListDataTable()
017.
Dim
objConn
As
OleDbConnection
018.
Dim
dtAdapter
As
OleDbDataAdapter
019.
Dim
dt
As
New
DataTable
020.
021.
Dim
strConnString
As
String
022.
strConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& _
023.
Server.MapPath(
"database/mydatabase.mdb"
)&
";"
024.
objConn =
New
OleDbConnection(strConnString)
025.
objConn.Open()
026.
027.
Dim
strSQL
As
String
028.
strSQL =
"SELECT * FROM country"
029.
030.
dtAdapter =
New
OleDbDataAdapter(strSQL, objConn)
031.
dtAdapter.Fill(dt)
032.
033.
dtAdapter =
Nothing
034.
objConn.Close()
035.
objConn =
Nothing
036.
037.
038.
With
Me
.myCBoxList1
039.
.DataSource = dt
040.
.DataTextField =
"CountryName"
041.
.DataValueField =
"CountryCode"
042.
.DataBind()
043.
End
With
044.
045.
046.
myCBoxList1.SelectedIndex = myCBoxList1.Items.IndexOf(myCBoxList1.Items.FindByValue(
"TH"
))
047.
048.
049.
End
Function
050.
051.
052.
Sub
CheckBoxListDataTableRows()
053.
Dim
dt
As
New
DataTable
054.
Dim
dr
As
DataRow
055.
056.
057.
dt.Columns.Add(
"Sex"
)
058.
dt.Columns.Add(
"SexDesc"
)
059.
060.
061.
dr = dt.NewRow
062.
dr(
"Sex"
) =
"M"
063.
dr(
"SexDesc"
) =
"Man"
064.
dt.Rows.Add(dr)
065.
066.
067.
dr = dt.NewRow
068.
dr(
"Sex"
) =
"W"
069.
dr(
"SexDesc"
) =
"Woman"
070.
dt.Rows.Add(dr)
071.
072.
073.
With
Me
.myCBoxList2
074.
.DataSource = dt
075.
.DataTextField =
"SexDesc"
076.
.DataValueField =
"Sex"
077.
.DataBind()
078.
End
With
079.
080.
081.
myCBoxList2.SelectedIndex = myCBoxList2.Items.IndexOf(myCBoxList2.Items.FindByValue(
"W"
))
082.
083.
084.
End
Sub
085.
086.
087.
Sub
CheckBoxListSortedList()
088.
Dim
mySortedList AS
New
SortedList
089.
090.
mySortedList.Add(
"M"
,
"Man"
)
091.
mySortedList.Add(
"W"
,
"Woman"
)
092.
093.
094.
With
Me
.myCBoxList3
095.
.DataSource = mySortedList
096.
.DataTextField =
"Value"
097.
.DataValueField =
"Key"
098.
.DataBind()
099.
End
With
100.
101.
102.
myCBoxList3.SelectedIndex = myCBoxList3.Items.IndexOf(myCBoxList3.Items.FindByValue(
"W"
))
103.
104.
End
Sub
105.
106.
107.
108.
Sub
CheckBoxListAddInsertItem()
109.
Dim
mySortedList AS
New
SortedList
110.
111.
mySortedList.Add(
"M"
,
"Man"
)
112.
mySortedList.Add(
"W"
,
"Woman"
)
113.
114.
115.
With
Me
.myCBoxList4
116.
.DataSource = mySortedList
117.
.DataTextField =
"Value"
118.
.DataValueField =
"Key"
119.
.DataBind()
120.
End
With
121.
122.
123.
124.
Dim
strText,strValue
As
String
125.
126.
127.
strText =
""
128.
strValue =
""
129.
Dim
InsertItem
As
New
ListItem(strText, strValue)
130.
myCBoxList4.Items.Insert(0, InsertItem)
131.
132.
133.
strText =
"Guy"
134.
strValue =
"G"
135.
Dim
AddItem
As
New
ListItem(strText, strValue)
136.
myCBoxList4.Items.Add(AddItem)
137.
138.
139.
myCBoxList4.SelectedIndex = myCBoxList4.Items.IndexOf(myCBoxList4.Items.FindByValue(
"W"
))
140.
141.
End
Sub
142.
143.
Sub
Button1_OnClick(sender as
Object
, e
As
EventArgs)
144.
145.
Dim
i
As
Integer
146.
147.
Me
.lblText1.Text =
""
148.
For
i = 0
To
Me
.myCBoxList1.Items.Count - 1
149.
If
Me
.myCBoxList1.Items(i).Selected
Then
150.
Me
.lblText1.Text =
Me
.lblText1.Text &
","
&
Me
.myCBoxList1.Items(i).Value
151.
End
If
152.
Next
153.
154.
Me
.lblText2.Text =
""
155.
For
i = 0
To
Me
.myCBoxList2.Items.Count - 1
156.
If
Me
.myCBoxList2.Items(i).Selected
Then
157.
Me
.lblText2.Text =
Me
.lblText2.Text &
","
&
Me
.myCBoxList2.Items(i).Value
158.
End
If
159.
Next
160.
161.
162.
Me
.lblText3.Text =
""
163.
For
i = 0
To
Me
.myCBoxList3.Items.Count - 1
164.
If
Me
.myCBoxList3.Items(i).Selected
Then
165.
Me
.lblText3.Text =
Me
.lblText3.Text &
","
&
Me
.myCBoxList3.Items(i).Value
166.
End
If
167.
Next
168.
169.
Me
.lblText4.Text =
""
170.
For
i = 0
To
Me
.myCBoxList4.Items.Count - 1
171.
If
Me
.myCBoxList4.Items(i).Selected
Then
172.
Me
.lblText4.Text =
Me
.lblText4.Text &
","
&
Me
.myCBoxList4.Items(i).Value
173.
End
If
174.
Next
175.
176.
End
Sub
177.
178.
</script>
179.
<html>
180.
<head>
181.
<title>ThaiCreate.Com ASP.NET - CheckBoxList & DataBind</title>
182.
</head>
183.
<body>
184.
<form id=
"form1"
runat=
"server"
>
185.
<asp:CheckBoxList id=
"myCBoxList1"
runat=
"server"
></asp:CheckBoxList><hr />
186.
<asp:CheckBoxList id=
"myCBoxList2"
runat=
"server"
></asp:CheckBoxList><hr />
187.
<asp:CheckBoxList id=
"myCBoxList3"
runat=
"server"
></asp:CheckBoxList><hr />
188.
<asp:CheckBoxList id=
"myCBoxList4"
runat=
"server"
></asp:CheckBoxList>
189.
<asp:Button id=
"Button1"
onclick=
"Button1_OnClick"
runat=
"server"
Text=
"Button"
></asp:Button>
190.
<hr />
191.
<asp:Label id=
"lblText1"
runat=
"server"
></asp:Label><br /><br />
192.
<asp:Label id=
"lblText2"
runat=
"server"
></asp:Label><br /><br />
193.
<asp:Label id=
"lblText3"
runat=
"server"
></asp:Label><br /><br />
194.
<asp:Label id=
"lblText4"
runat=
"server"
></asp:Label><br /><br />
195.
</form>
196.
</body>
197.
</html>