Dynamically created Tablerow/Tablecell/Checkbox. Expecting user to check/uncheck box then push separate execute btn. No matter where I put my interrogation code I am always getting 'false' result. I have reduced it down to the simplest form in a test project below. This interrogation code errors with index out of range as the programmatic row hasn't transported with the system variable. 'Hard coded' checkbox inside of hard coded row behaves as I would expect so I am missing something pretty 开发者_如何学JAVAbasic. thanks Bob
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
CheckState();
else
{
MakeARow();
}
}
private void MakeARow()
{
TableRow r = new TableRow();
TableCell c = new TableCell();
CheckBox k = new CheckBox();
c.Controls.Add(k);
r.Cells.Add(c);
Table1.Rows.Add(r);
}
private void CheckState()
{
Table t = (Table) Session["MyTable"];
if (((CheckBox) t.Rows[0].Cells[0].Controls[0]).Checked)
this.Label1.Text = "Checked";
else
{
this.Label1.Text = "UnChecked";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["MyTable"] = Table1;
}
You have to create the row before Page_Load event, i.e. in Page_Init. For more details you can check the documentation for ASP.NET page life cycle - http://msdn.microsoft.com/en-us/library/ms178472.aspx
精彩评论