开发者

Page_Init and Page_Load

开发者 https://www.devze.com 2023-03-21 20:10 出处:网络
A page containts custom address control and checkBox. Why does the second example of code work properly, but first doesn\'t?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消