1

我实现 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 处理程序

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 方法找到我的复选框控件:

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

它抛出一个异常,因为 gvLicence.Rows[e.RowIndex].FindControl("ckRequest") 为空。

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

4

0 回答 0