Page_Init and Page_Load
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
ucLegalAddre开发者_如何学Css.Visible = !chkLegalAddress.Checked;
}
//2
protected void Page_Load(object sender, EventArgs e)
{
//works properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
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.
MSDN lifecycle overview
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
精彩评论