01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Linq;
04.
using
System.Web;
05.
using
System.Web.UI;
06.
using
System.Web.UI.WebControls;
07.
08.
using
System.Data;
09.
10.
public
partial
class
MyDataList : System.Web.UI.Page
11.
{
12.
protected
void
Page_Load(
object
sender, EventArgs e)
13.
{
14.
DataList1.DataSource = GetDataForDataList();
15.
DataList1.ItemDataBound +=
new
DataListItemEventHandler(DataList1_ItemDataBound);
16.
DataList1.DataBind();
17.
}
18.
19.
protected
void
DataList1_ItemDataBound(
object
sender, DataListItemEventArgs e)
20.
{
21.
DataRowView Dr = (DataRowView)e.Item.DataItem;
22.
23.
if
(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
24.
{
25.
((Label)e.Item.FindControl(
"Label1"
)).Text = (((
int
)Dr[
"ID"
]) + 1).ToString() +
". tungman"
;
26.
((RadioButtonList)e.Item.FindControl(
"RadioButtonList1"
)).DataSource = GetDataForRadioButtonList();
27.
((RadioButtonList)e.Item.FindControl(
"RadioButtonList1"
)).DataTextField =
"Name"
;
28.
((RadioButtonList)e.Item.FindControl(
"RadioButtonList1"
)).DataValueField =
"ID"
;
29.
((RadioButtonList)e.Item.FindControl(
"RadioButtonList1"
)).DataBind();
30.
}
31.
}
32.
33.
34.
private
DataTable GetDataForDataList()
35.
{
36.
DataTable Dt =
new
DataTable();
37.
Dt.Columns.Add(
new
DataColumn(
"ID"
, System.Type.GetType(
"System.Int32"
)));
38.
39.
for
(
int
i = 0; i < 3; i++)
40.
{
41.
DataRow Dr = Dt.NewRow();
42.
Dr[
"ID"
] = i;
43.
Dt.Rows.Add(Dr);
44.
}
45.
46.
return
Dt;
47.
}
48.
49.
50.
private
DataTable GetDataForRadioButtonList()
51.
{
52.
DataTable Dt =
new
DataTable();
53.
Dt.Columns.Add(
new
DataColumn(
"ID"
, System.Type.GetType(
"System.Int32"
)));
54.
Dt.Columns.Add(
new
DataColumn(
"Name"
, System.Type.GetType(
"System.String"
)));
55.
56.
for
(
int
i = 0; i < 5; i++)
57.
{
58.
DataRow Dr = Dt.NewRow();
59.
Dr[
"ID"
] = i;
60.
Dr[
"Name"
] =
"Item"
+ i.ToString();
61.
Dt.Rows.Add(Dr);
62.
}
63.
64.
return
Dt;
65.
}
66.
}