findcontrol does not find dynamically created control in rowUpdating eventhandler

。_饼干妹妹 提交于 2019-12-11 04:17:17

问题


I implemten ITemplate to dynamically create a Template field

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

I implement 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);

...

        }

with the 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);
}

But then in the RowUpdating Handler I cannot find my checkBox Control with the FindControl Method:

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

It throws an Exeption because gvLicence.Rows[e.RowIndex].FindControl("ckRequest") is null.

Many thanks for your attention and help.

来源:https://stackoverflow.com/questions/14874940/findcontrol-does-not-find-dynamically-created-control-in-rowupdating-eventhandle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!