遍历GridView

原文:http://jiafulc.i.sohu.com/blog/view/95151585.htm

1、遍历GridView 
foreach (GridViewRow row in GridView1.Rows)
   {
        string mylable = ((Label)row.FindControl("Label1")).Text;
       Response.Write(mylable);
   }

2、遍历DataList 
foreach (DataListItem dl in this.dlWebSite.Items)
        {
            CheckBox cb = (CheckBox)dl.FindControl("chbWebSite");
            if (cb.Checked)
            {
                ...           
            }


        }

GridView 遍历每一行,有两种写法。
方法一:
for (i = 0; i < GridView1.Rows.Count; i++)
{
   CheckBox chkVote =  (CheckBox)GridView1.Rows[i].FindControl("CheckBox2");
}

方法二:
foreach (GridViewRow row in GridView1.Rows)
{
    CheckBox chkVote = (CheckBox)row.FindControl("chkVote");
}

两种方法没有太大差别,但是如果你股知道GridViewRow这个类型就很难想到使用这种方法。类似的DataList。 
foreach (DataListItem dl in this.dlWebSite.Items)
{
    CheckBox chkVote = (CheckBox)row.FindControl("chkVote");
}

GridView 遍历每一行,有两种写法。
方法一:
for (i = 0; i < GridView1.Rows.Count; i++)
{
   CheckBox chkVote =  (CheckBox)GridView1.Rows[i].FindControl("CheckBox2");
}

方法二:
foreach (GridViewRow row in GridView1.Rows)
{
    CheckBox chkVote = (CheckBox)row.FindControl("chkVote");
}

两种方法没有太大差别,但是如果你股知道GridViewRow这个类型就很难想到使用这种方法。类似的DataList。 
foreach (DataListItem dl in this.dlWebSite.Items)
{
    CheckBox chkVote = (CheckBox)row.FindControl("chkVote");
}



已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页