I've created a server control that renders a list of check boxes. The problem is that when I check some of the checkboxes and postback form all the check boxes gets unchecked. Here is my server control class:
public class WeekControl : WebControl
{
public bool ShowCheckBoxs { get; set; }
public WeekControl()
{
ShowCheckBoxs = true;
Table = new Table();
Table.ID = "table1";
CheckBoxes = new CheckBox[7, 4];
Table.CssClass = "weekly";
}
protected override void OnInit(EventArgs e)
{
for (int i = 0; i < 6; i++)
{
TableRow tr = new TableRow();
Table.Rows.Add(tr);
for (int j = 0; j < 4; j++)
{
TableCell tc = new TableCell();
tr.Cells.Add(tc);
if (ShowCheckBoxs)
{
CheckBoxes[i, j] = new CheckBox();
CheckBoxes[i, j].ID = "ch" + i.ToString() + j.ToString();
tc.Controls.Add(CheckBoxes[i, j]);
}
}
}
}
public CheckBox[,] CheckBoxes;
public Table Table;
protected override void CreateChildControls()
{
Controls.Add(Table);
ChildControlsCreated = true;
base.CreateChildControls();
}
}
Any ideas?
Perhaps CreateChildControls
comes after the step that deserializes the viewstate. Have you tried moving Controls.Add(Table)
to OnInit
?
You might find the MSDN article on ASP.NET page life cycle an interesting read. It mentions CreateChildControls only in the comment section.
The only comment on the CreateChildControls documentation page cautions against overriding the method. If it helps you, buy Roy Soliver a beer :)