在FindControl已rowUpdating事件处理程序没有找到动态创建的控制(findcont

2019-10-17 17:01发布

我implemten了Itemplate动态创建一个模板场

TemplateField isReqField = new TemplateField();
isReqField.HeaderText = "Lizenz anfordern";
isReqField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, DataControlRowState.Normal, "isRequested", "bool");
isReqField.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, DataControlRowState.Edit, "isRequested", "bool");

gvLicence.Columns.Add(isReqField);

我实现InstantiateIn

        public void InstantiateIn(System.Web.UI.Control container)
        {

... 

                            CheckBox ckRequest = new CheckBox();
                            ckRequest.ID = "ckRequest";
                            ckRequest.DataBinding += new EventHandler(this.CkIsRequested_DataBinding);
                            container.Controls.Add(ckRequest);

...

        }

与数据绑定处理程序

private void CkIsRequested_DataBinding(Object sender, EventArgs e)
{

    CheckBox ckRequest = (CheckBox)sender;
    GridViewRow row = (GridViewRow)ckRequest.NamingContainer;
    ckRequest.Checked = (bool)DataBinder.Eval(row.DataItem, columnName);
}

但随后在程序的RowUpdating处理程序我找不到我的FindControl方法CheckBox控件:

protected void gvLicence_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    CheckBox chb = (CheckBox)gvLicence.Rows[e.RowIndex].FindControl("ckRequest");
    bool requestValue = chb.Checked;

它抛出一个Exeption因为gvLicence.Rows [e.RowIndex] .FindControl( “ckRequest”)为空。

非常感谢您的关注和帮助。

文章来源: findcontrol does not find dynamically created control in rowUpdating eventhandler