Reputation: 13318
A page containts custom address control and checkBox. Why does the second example of code work properly, but first doesn't?
//1
protected void Page_Init(object sender, EventArgs e)
{
//doesn't work properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
//2
protected void Page_Load(object sender, EventArgs e)
{
//works properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
Upvotes: 12
Views: 15119
Reputation: 27944
Because the viewstate of the controls is loaded between the init and the load event. This means that the init event does not know the state of the client yet.
Upvotes: 11
Reputation: 1010
Because all controls are create in OnInit() method, that call between Page_Init and Page_Load. In Page_Init all controls are null. Read more
Upvotes: 2