Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
   }
like image 803
Alexandre Avatar asked Jul 19 '11 07:07

Alexandre


People also ask

What is the difference between Page_Load and Page_Init?

Note that the Page_Init event fires only the first time the page is loaded. When you use a web form and post back to this page again, the Page_Init event doesn't fire. But the Page_Load event fires each time the page loads.

What is Page_Load?

Your web form has a method called Page_Load. This method is called each time your page is loaded. This includes: 1. The first time the page is loaded, e.g., when a user enters the url to it or clicks on a link to it.

What is a Page_Load event?

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded , which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.

Is Page_Init called on postback?

Yes, page_init and page_load, both will fire during postback.


1 Answers

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

like image 141
Peter Avatar answered Sep 24 '22 15:09

Peter