Alexandre
Alexandre

Reputation: 13318

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
   ucLegalAddress.Visible = !chkLegalAddress.Checked;
}


 //2
 protected void Page_Load(object sender, EventArgs e)
  {
    //works properly
     ucLegalAddress.Visible = !chkLegalAddress.Checked;
   }

Upvotes: 12

Views: 15119

Answers (2)

Peter
Peter

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.

MSDN lifecycle overview

Upvotes: 11

Sergey Shulik
Sergey Shulik

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

Related Questions