如何检查动态创建的复选框是否选中?

Ali*_*jum 5 c# asp.net

我在表格标题行中创建了一些动态复选框。

如果复选框被选中,表格将有另一行带有文本框。

复选框更改事件触发得很好但是当我尝试检查复选框是否被选中时,它会生成异常:

你调用的对象是空的。

这是我的代码:

在此处创建动态控件:

 TableRow thead = new TableRow();
 for (int i = 0; i < dt_fundtype.Rows.Count; i++)
 {
     TableCell td = new TableCell();
     CheckBox chk = new CheckBox();
     chk.ID = "fund_" + dt_fundtype.Rows[i]["fund_type_cd"];
     chk.Text = dt_fundtype.Rows[i]["fund_type"].ToString();
     chk.AutoPostBack = true;
     chk.CheckedChanged += new EventHandler(Chk_Fund_CheckedChange);
     td.Controls.Add(chk);
     thead.Cells.Add(td);
 }
 tbl_fundtype.Rows.Add(thead);
Run Code Online (Sandbox Code Playgroud)

复选框选中更改事件:

public void Chk_Fund_CheckedChange(object sender, EventArgs e)
{
     TableRow tr = new TableRow();
     for (int i=0;i<tbl_fundtype.Rows[0].Cells.Count;i++)
     {
         string DynamicChkID ="fund_"+ dt_fundtype.Rows[i]["fund_type_cd"].ToString();
         CheckBox chk = new CheckBox();
         chk = (CheckBox)tbl_fundtype.Rows[0].Cells[i].FindControl("DynamicChkID");
         if (chk.Checked == true)//Here is the Exception
         {
             TableCell td = new TableCell();
             td.Text = "Test";
             tr.Cells.Add(td);
         }
     }

     tbl_fundtype.Rows.Add(tr);
     hfTab.Value = "fund";
     collapsestate = "expand";
}
Run Code Online (Sandbox Code Playgroud)

事件触发得很好,但是当我检查复选框是否被选中时,会出现异常。

我该如何解决这个问题?请帮助我

All*_*sen 4

这是我使用的对我有用的示例。
我简化了绑定逻辑和查找逻辑等,以说明

protected void Page_Load(object sender, EventArgs e)        
{
    AddStuff();
}

private void AddStuff()
{
    TableRow thead = new TableRow();
    for (int i = 0; i < 10; i++)
    {
        TableCell td = new TableCell();
        CheckBox chk = new CheckBox();
        chk.ID = "fund_" + i;
        chk.Text = i.ToString();
        chk.AutoPostBack = true;
        chk.CheckedChanged += new EventHandler(Chk_Fund_CheckedChange);
        td.Controls.Add(chk);
        thead.Cells.Add(td);
    }
    tbl_fundtype.Rows.Add(thead);
}

public void Chk_Fund_CheckedChange(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        var chk = tbl_fundtype.FindControl("fund_" + i) as CheckBox;
        if (chk.Checked)
        {
            //I am checked
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的标记页面上,我有:
<asp:Table ID="tbl_fundtype" runat="server" />