2相同的代码,1将无效

时间:2014-04-10 12:15:18

标签: c# asp.net gridview linq-to-sql

问题:

我有两个网格控件,我正在迭代,一个是按预期运行,另一个不是。这些片段分别位于两个页面上

工作示例

以下示例包含大约10个数据,无问题,并且在页面呈现时显示所有更改。

protected void LoadEmployeeAccess()
{
    gridEmployeeAccess.DataSource = TAClass
                                     .GetAllEmployeesWithEmail2(Globals.EmpNo);
    gridEmployeeAccess.DataBind();


    foreach (GridViewRow row in gridEmployeeAccess.Rows)
    {
        var targetControl = row.Cells[0].Controls[0];

        Button btnEdit = targetControl.FindControl("btnEdit") as Button;

        Label lblGroup = targetControl.FindControl("lblGroup") as Label;

        Label lblRole = targetControl.FindControl("lblRole") as Label;

        Label lblEmployeeNumber = targetControl
                                    .FindControl("lblEmployeeNumber") as Label;

        bool isAdmin = TAClass.CheckIfAdmin(Globals.EmpNo, lblGroup.Text);

        if(isAdmin)
            btnEdit.Visible = true;

    }             

}

非工作示例

不工作的代码只有两个样本数据,在第一次迭代中找到labelEditGroupName罚款,在第二次迭代中我没有得到控制但我发现btnEdit

protected void gridThreshold_RowEditing(object sender, GridViewEditEventArgs e)
{
   gridThreshold.EditIndex = e.NewEditIndex;
   gridThreshold.ShowFooter = false;
   //LoadInventory();
   gridThreshold.DataSource = TAClass.ThresholdList(Globals.EmpNo);
   gridThreshold.DataBind();            

   GridViewRow item = gridThreshold.Rows[gridThreshold.EditIndex];  

   Button btnSave = (Button)item.FindControl("btnSave");     
   Button btnCancel = (Button)item.FindControl("btnCancel");
   Label lblEditLimit = (Label)item.FindControl("lblEditLimit");
   TextBox txtEditThreshold = (TextBox)item.FindControl("txtEditThreshold");

   btnCancel.Visible = true;
   btnSave.Visible = true;
   lblEditLimit.Visible = true;


   foreach (GridViewRow row2 in gridThreshold.Rows)
   {
       var targetControl = row2.Cells[0].Controls[0];

       Button btnEdit = targetControl.FindControl("btnEdit") as Button;

       Label lblEditGroupName = targetControl
                                  .FindControl("lblEditGroupName") as Label;

       bool isAdmin = TAClass.CheckIfAdmin(Globals.EmpNo, lblEditGroupName.Text);

       btnEdit.Visible = isAdmin;

    }
}   

请帮助我知道我哪里出错了。

提前致谢!

0 个答案:

没有答案