本文實例講述了ASP.NET中GridView、DataList、DataGrid三個數據控件foreach遍歷用法。分享給大家供大家參考,具體如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//gridview遍歷如下: foreach (GridViewRow row in GridView1.Rows) { CheckBox cb = (CheckBox)row.FindControl( "CheckBox2" ); if (cb.Checked == true ) { } } //datagrid遍歷: foreach (DataGridItem oItem in ItemsGrid.Items) { CheckBox ck1 = (CheckBox)oItem.FindControl( "CheckBox" ); if (ck1.Checked == true ) { } } //datalist遍歷 foreach (DateListItem dl in DataList1.Items) { CheckBox cb = (CheckBox)dl.FindControl( "CheckBoxID " ); if (cb. checked ) ...... } |
GridView:
1
2
3
4
|
for ( int i=0; i <GridView.Rows.Count;i++) { Label lbl = (Label)GridView.Rows[i].FindControl( "Label9" ); } |
Repeater:
1
2
3
4
|
for ( int i=0; i < Repeater.Items.Count;i++) { Label lbl = (Label) Repeater.Items[i].FindControl( "Label9" ); } |
DataList:
1
2
3
4
|
for ( int i=0; i < DataList.Items.Count;i++) { Label lbl = (Label) DataList.Items[i].FindControl( "Label9" ); } |
希望本文所述對大家asp.net程序設計有所幫助。