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

时间:2018-01-28 12:28:11

标签: c# asp.net gridview

我正在为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的代码如下:

 private void BindGridView(DataTable dtData)
        {
            foreach (DataColumn item in dtData.Columns)
            {
                TemplateField tfield = new TemplateField();
                tfield.HeaderText = item.ToString();
                grdData.Columns.Add(tfield);
            }
            grdData.DataSource = dtData;
            ViewState["dtDataTable"] = dtData;
            grdData.DataBind();

        }

并在行数据绑定中我将文本框和标签添加到模板字段:

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

代码行下方抛出对象引用错误,因为它们无法找到控件

 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;
        }

编辑:

我尝试在preinit事件中重新启动控件,但仍然没有运气:

protected void Page_PreInit(object sender, EventArgs e)
        {

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我在OnRowCreated中重新创建了动态gridview控件,因为在每个回发中调用此事件而不是onRowDataBound事件,它就像魅力一样。