Gridview中的Gridview查找控件

时间:2013-05-13 06:17:28

标签: asp.net webforms

如何在用户定义的函数中找到gridview的控件 ... 抛出

DataSet ds = objSelectAll.Paging(PageSize, PageNumber, USERID, ROLEID);

if (Session["Username"].ToString() == "admin")
{ 
    foreach (GridViewRow row in UserRoleGridView.Rows)
    {
        ImageButton ImgEditbtn = (ImageButton)row.FindControl("EditButton");
        ImageButton ImgDelbtn = (ImageButton)row.FindControl("DeleteButton");

        DataSet dsusr = objSelectAll.UserBasedPaging(PageSize, PageNumber, USERID, ROLEID);
        UserRoleGridView.DataSource = dsusr.Tables[1];
        UserRoleGridView.DataBind();

        ...

2 个答案:

答案 0 :(得分:0)

您需要检查当前行是数据行还是标题行。

用于检查您需要编写的行类型

 if(row.RowType == DataControlRowType.DataRow) {
// do what ever you want
}

现在您的代码看起来像

  foreach(GridViewRow row in GridView1.Rows) {
        if(row.RowType == DataControlRowType.DataRow) {
      // do what ever you want

    }
}

我希望它能解决你的问题。

答案 1 :(得分:0)

尝试这个

foreach (GridViewRow row in UserRoleGridView.Rows)
                {
                    ImageButton ImgEditbtn = (ImageButton)row[row.RowIndex].FindControl("EditButton");
                    ImageButton ImgDelbtn = (ImageButton)row[row.RowIndex].FindControl("DeleteButton");
                DataSet dsusr = objSelectAll.UserBasedPaging(PageSize, PageNumber, USERID, ROLEID);
                UserRoleGridView.DataSource = dsusr.Tables[1];
                UserRoleGridView.DataBind();
    }