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

时间:2016-05-30 05:53:53

标签: 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);

复选框已选中已更改的事件:

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";
}

事件发生得非常好,但当我检查复选框是否已选中时,会出现例外情况。

如何解决此问题?请帮助我

2 个答案:

答案 0 :(得分:3)

这是我用来为我工作的例子。
我简化了绑定逻辑和查找逻辑等,以说明

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
        }
    }
}

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

答案 1 :(得分:1)

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = Rowcreation();

    TableRow thead = new TableRow();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        TableCell td = new TableCell();
        CheckBox chk = new CheckBox();
        chk.ID = "fund" + dt.Rows[i]["ID"];
        chk.Text = dt.Rows[i]["Name"].ToString();    

        chk.AutoPostBack = true;
        chk.CheckedChanged += new EventHandler(chk_fun_checkedchange);
        td.Controls.Add(chk);
        thead.Controls.Add(td);
    }
    pnl.Controls.Add(thead);   
}

public void chk_fun_checkedchange(object sender, EventArgs e)
{
    DataTable dt = Rowcreation();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        CheckBox chkbx = (CheckBox ) pnl.Parent.Controls[0].FindControl("fund"+dt.Rows [i]["ID"]);
        if (chkbx.Checked == true)
        {
            lbl.Text = chkbx.Text;
        }
    }
}

private DataTable Rowcreation()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Name");
    DataRow dr = dt.NewRow();
    dr["ID"] = "1";
    dr["Name"] = "Name1";
    dt.NewRow();
    dt.Rows.Add(dr);

    DataRow dr1 = dt.NewRow();
    dr1["ID"] = "2";
    dr1["Name"] = "Name2";
    dt.Rows.Add(dr1);

    dt.NewRow();
    DataRow dr2 = dt.NewRow();
    dr2["ID"] = "3";
    dr2["Name"] = "Name3";
    dt.Rows.Add(dr2);

    return dt;
}
相关问题