ASP.Net - Page_Load and Page_Init get called on each button click?

十年热恋 提交于 2019-12-10 15:04:13

问题


So I have absolutely no logic in my code. I have the two methods Page_Init & Page_Load

Both methods get called every time I click the button. It makes sense for Page_load to get called. But why does Page_Init get called every time?

protected void Page_Init(Object sender, EventArgs e)
{

}

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click1(object sender, EventArgs e)
{
    // Do something here
}

回答1:


I guess you are unaware of Page Life Cycle.

Page_Init will always be called on page creation and it is called before Page Load.

Pre Init is called then Init is called and then Pre Load and then Load and then Pre Render and then Render on almost every time postback happens.

You can do this trick if you are not on live server. Add

Trace ="true"

in Page directive that will show you complete page cycle.

Like this,

   <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 
    Inherits="_Default" Trace="true"%>


来源:https://stackoverflow.com/questions/7479976/asp-net-page-load-and-page-init-get-called-on-each-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!