繁体   English   中英

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

[英]Not able to access dynamically generated templated field controls

我正在为我的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();

        }

在数据绑定的行中,我将文本框和标签添加到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

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

 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"]);
            }
        }

我做错了什么?

我在OnRowCreated中重新创建了动态gridview控件,因为此事件在每次回发中都被调用,而不是在onRowDataBound事件中被调用,并且它的工作原理就像是魅力。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM