Example of foreach traversal usage of GridView DataList and DataGrid data controls in ASP. NET

  • 2021-08-05 09:36:52
  • OfStack

This paper describes the traversal usage of foreach in ASP. NET, GridView, DataList and DataGrid. Share it for your reference, as follows:


//gridview The traversal is as follows:  
foreach (GridViewRow row in GridView1.Rows)
{
  CheckBox cb = (CheckBox)row.FindControl("CheckBox2");
  if (cb.Checked == true)
  {
  }
}
//datagrid Traversal: 
foreach (DataGridItem oItem in ItemsGrid.Items)
{
  CheckBox ck1 = (CheckBox)oItem.FindControl("CheckBox");
  if (ck1.Checked == true)
  {
  }
}
//datalist Traversal 
foreach (DateListItem dl in DataList1.Items)
{
  CheckBox cb = (CheckBox)dl.FindControl("CheckBoxID ");
  if(cb.checked)
  ......
}

GridView:


for(int i=0; i <GridView.Rows.Count;i++)
{
   Label lbl = (Label)GridView.Rows[i].FindControl("Label9") ; 
}

Repeater:


for(int i=0; i < Repeater.Items.Count;i++)
{
  Label lbl = (Label) Repeater.Items[i].FindControl("Label9") ; 
}

DataList:


for(int i=0; i < DataList.Items.Count;i++)
{
  Label lbl = (Label) DataList.Items[i].FindControl("Label9") ; 
}

More readers interested in asp. net can check the topics of this site: "Summary of asp. net Operation json Skills", "Summary of asp. net String Operation Skills", "Summary of asp. net Operation XML Skills", "Summary of asp File Operation Skills", "Summary of asp. net ajax Skills" and "Summary of asp. net Cache Operation Skills".

I hope this article is helpful to everyone's asp. net programming.


Related articles: