C# 无法访问动态生成的模板化字段控件

C# 无法访问动态生成的模板化字段控件,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在为我的gridview创建动态模板字段: <asp:GridView ID="grdData" runat="server" DataKeyNames = "ID" AutoGenerateColumns="false" OnRowDataBound="grdData_RowDataBound"> <Columns> <asp:TemplateField> <HeaderTemplate> <

我正在为我的gridview创建动态模板字段:

 <asp:GridView ID="grdData"  runat="server" DataKeyNames = "ID" AutoGenerateColumns="false" OnRowDataBound="grdData_RowDataBound">
     <Columns>
       <asp:TemplateField>
   <HeaderTemplate>
     <asp:CheckBox ID="chkAll" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" runat="server" />
        </HeaderTemplate>
             <ItemTemplate>
                <asp:CheckBox ID="editbtn" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" runat="server"  />
             </ItemTemplate>
         </asp:TemplateField>
       </Columns>
   </asp:GridView>
在数据绑定行中,我将文本框和标签添加到templatefield:

protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            DataTable dtData = (DataTable)ViewState["dtDataTable"];
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = 1;
                foreach (DataColumn item in dtData.Columns )
                {
                    TextBox txtBox = new TextBox();
                    txtBox.ID = "txt"+item.ToString();
                    txtBox.Text = (e.Row.DataItem as DataRowView).Row[item.ToString()].ToString();
                    txtBox.Visible = false;
                    e.Row.Cells[i].Controls.Add(txtBox);

                    Label lblBox = new Label();
                    lblBox.ID = "lbl" + item.ToString();
                    lblBox.Text = (e.Row.DataItem as DataRowView).Row[item.ToString()].ToString();
                    e.Row.Cells[i].Controls.Add(lblBox);
                    i++;
                }

            }
        }
到目前为止,一切正常,网格正在创建,值正在填充,但当我调用下面的方法并尝试访问gridview控件时,其抛出对象引用错误:

protected void OnCheckedChanged(object sender, EventArgs e)
        {
            bool isUpdateVisible = false;
            CheckBox chk = (sender as CheckBox);
            if (chk.ID == "chkAll")
            {
                foreach (GridViewRow row in grdData.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
                    }
                }
            }
            CheckBox chkAll = (grdData.HeaderRow.FindControl("chkAll") as CheckBox);
            chkAll.Checked = true;
            foreach (GridViewRow row in grdData.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
                    for (int i = 1; i < row.Cells.Count; i++)
                    {
                        Label test= row.FindControl("lblName") as Label;//this is coming null

我做错了什么?

我在OnRowCreated中重新创建了动态gridview控件,因为每次回发时都会调用此事件,而不是onRowDataBound事件,而且效果很好。

@rene我尝试重新创建控件,请查看我的编辑,仍然没有成功。
protected void OnCheckedChanged(object sender, EventArgs e)
        {
            bool isUpdateVisible = false;
            CheckBox chk = (sender as CheckBox);
            if (chk.ID == "chkAll")
            {
                foreach (GridViewRow row in grdData.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
                    }
                }
            }
            CheckBox chkAll = (grdData.HeaderRow.FindControl("chkAll") as CheckBox);
            chkAll.Checked = true;
            foreach (GridViewRow row in grdData.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
                    for (int i = 1; i < row.Cells.Count; i++)
                    {
                        Label test= row.FindControl("lblName") as Label;//this is coming null
 row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;//this line throwing object reference error
                        if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
                        {
                            row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
                        }
                        if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
                        {
                            row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
                        }
                        if (isChecked && !isUpdateVisible)
                        {
                            isUpdateVisible = true;
                        }
                        if (!isChecked)
                        {
                            chkAll.Checked = false;
                        }
                    }
                }
            }
            btnUpdate.Visible = isUpdateVisible;
        }
protected void Page_PreInit(object sender, EventArgs e)
        {

            if (ViewState["gridData"] != null)
            {
                BindGridView((DataTable)ViewState["gridData"]);
            }
        }