If you have Panel and Button on Page like:
<form id="Form1" method="post" runat="server">
<asp
anel ID="Panel1" Runat="server" /><asp:Button id="Button1"
style="Z-INDEX: 101; LEFT: 18px; POSITION: absolute; TOP: 54px"
runat="server" Text="Button"></asp:Button>
</form>
Then on Page_Load:
private void Page_Load(object sender, System.EventArgs e)
{
//Creating CheckBoxes
for (int i=1;i<3;i++)
{
CheckBox B1 = new CheckBox();
B1.ID = "Check"+i;
B1.Text = "Check"+i;
B1.Visible = true;
B1.AutoPostBack = true;
Panel1.Controls.Add(B1);
}
}
And on Button Click:
private void Button1_Click(object sender, System.EventArgs e)
{
foreach (Control c in Panel1.Controls)
{
CheckBox chk=c as CheckBox;
if(chk != null)
{
if (chk.Checked)
{
Response.Write ("Checked");
}
}
}
}
So as a result you actually can't read the Checked property ftrom dynamical
CheckBoxes on Page_load because postback data is loaded after Page_Load for
dynamical controls. The lifecycle is as I told in a thread at ASP.NET
Forums.
1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load
8. Raise Changed Events (postback,
IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose
And it is the 7th that explains if control are created at Page_Load theyr
postback data is not available yet. Another solution certainly could be that
create ChekBoxes in Page_INit (Initialize phase) when postback data should
be loaded before Page_Load (as the cycle explains)
--
Teemu Keiski
MCP,Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi
AspInsiders Member,
www.aspinsiders.com
ASP.NET Forums Moderator,
www.asp.net
AspAlliance Columnist,
www.aspalliance.com