R R
R R

Reputation: 2956

Not able to access dynamically generated templated field controls

I am creating dynamic Template Fields for my 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>

the code to add templateField is below:

 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();

        }

and in row databound I am adding textbox and label to the 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++;
                }

            }
        }

Everything is working good so far,The grid is getting created and the values are getting populated ,but when i am calling below method and try to access the gridview control ,its throwing object reference error:

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

Below code lines are throwing object reference error as they are not able to find control

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

Edit:

I tried reinistialising the controls in preinit event but still no luck:

protected void Page_PreInit(object sender, EventArgs e)
        {

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

What I am doing wrong?

Upvotes: 0

Views: 146

Answers (1)

R R
R R

Reputation: 2956

I recreated the dynamic gridview Controls in OnRowCreated as this event gets called in every postback instead of onRowDataBound Event and it worked like charm.

Upvotes: 1

Related Questions