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