findcontrol在rowUpdating eventhandler中找不到动态创建的控件

时间:2013-02-14 12:31:23

标签: asp.net findcontrol itemplate

我实现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);

...

        }

使用DataBinding Handler

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 Handler中我找不到使用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”)为null。

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

0 个答案:

没有答案