I've created some dynamic checkboxes in table header row.
If the checkbox is checked , table will have another row with a textbox.
Checkbox changed event is firing very well but when I'm trying to check whether the checkbox is checked or not , it's generating exception :
Object Reference not set to an instance of an object.
Here is my code :
Creating Dynamic controls here :
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);
Checkbox checked changed event :
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";
}
The event is firing very well , but when I check that the checkbox is checked or not there's an exception.
How can I resolve this problem ? Kindly Help me Please
This is the example I use which work for me.
I've simplified the binding logic and lookup logic etc, to illustrate
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
}
}
}
And on my markup page I have:<asp:Table ID="tbl_fundtype" runat="server" />
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With