When a request is sent to the server, the following events will fire
Below is the Order of the Events.
protected void Page_PreInit(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_InitComplete(object sender, EventArgs e)
{
}
protected void Page_PreLoad(object sender, EventArgs e)
{
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
}
protected void Page_PreRender(object sender, EventArgs e)
{
}
Output:
PreInit
In this event we can access to master pages and themes.
You can dynamically set the values of master pages and themes in this event.
You can also dynamically create controls in this event.
Note:
Set a master page dynamically.
Set the Theme property dynamically.
protected void Page_PreInit(object sender, EventArgs e)
Example:
Page.MasterPageFile = "~/MasterPage.master";
TextBox masterTextBox = (TextBox)Page.Master.FindControl("txtName");
Label mastLabel = (Label)Page.Master.FindControl("lblErrorMessage");
mastLabel .Text = "Hi, this is Uday";
masterTextBox .Text = "Hi, this is TextBox";
File->Add New Item
MasterPage
Write the Following Code in MasterPage Design as Follows:
Now, Add New Item
Here, I have created a new page Default5 and using the master page dynamically from code behind
Design the Page as follows:
.CS
Now, Run the Program and see the output
Now, my doubt is can i set this master page dynamically in other event.
So, i tried to set this master page in other event after Page_PreInit event, so i am getting an error as
Now, i am copying the same code and pasting in the next event which is
protected void Page_Init(object sender, EventArgs e)
Now, i am getting an error as follows.
Init
This event fires after each control has been initialized.
You can use this event to change initialization values for controls.
we can use this to read the property of each initialized control .
We can Set Unique Id and skin properties of the controls.
We will not get the post back values of the controls.
protected void Page_PreInit(object sender, EventArgs e)
Example:
Suppose you have entered some text in a textbox control and clicking on a button control that makes a postback, now you want to view the value that you have entered. In such case you will be able to see the textbox value which you’ve entered.
.cs page
Output(Runing Mode)
Now, i Enter some text and click on Button
Example 2
.cs Page
OUTPUT:
On Button Click Debugging the Code to see the View State Values
Explanation:
Here on Button Click i am saving the value ViewState["a"]
and when i try to access the value in Page_Init, it does not display the VALUE From ViewState
So in Init we will not get the Post Back Values
InitComplete
Initialization process is completed in this section.
Tracking of View State can be done in this event.
Till now the viewstate values are not yet loaded, We can make changes to ViewState in this event.
protected void Page_InitComplete(object sender, EventArgs e)
PreLoad :
View State data is visible after postback also.
This event will always Fires after the InitComplete Event
From here viewstate functionality starts and we can retrieving their values.
.cs Page
Run the program, and enter any value in text box and click on button click, now, the view state has a value, Now again click on the button and see the output in debugging mode as follows
In PreLoad
In which event controls get loaded
In, Page load event all controls are fully loaded. But Controls can be accessed in Page_Init events but you will see that view state is not fully loaded during this event.
Load
We can validate Page.
You can create dynamic controls in this method.
Controls Events
This event is used to handle control events such as a Button control’s Click event or a TextBox control’s TextChanged event.
If you have a control like button, and you clicked on the Button,so after the Page_Load event your Button_Click event will fire.
Example
Output:
After Button Click
LoadComplete :
The loading process is completed, page validation takes place.
protected void Page_LoadComplete(object sender, EventArgs e)
{
lblmessage.Text += "Page_LoadComplete<br/>";
}
OnPreRender
Here we can have last changes to the page or its control.
This event takes place before saving ViewState, so any changes made here are saved.
For example: After completion of this event , we cannot change any property of a button or change any viewstate value
Use the event to make last changes to the contents of the page or its controls.
Any change will be stored on the page.
protected void Page_PreRender(object sender, EventArgs e)
PreRenderComplete .
this event is the completion of the pre-rendering phase.
SaveStateComplete
The next processed method is SaveViewState(). This method saves the updated Viewstate to be processed on the next page. The final Viewstate is encoded to the _viewstate hidden field on the page during the page render.
Before this event occurs, ViewState has been saved for the page and for all controls.
Any changes to the page or controls at this point will be not done.
If you change the server control then state will not be available in next Post Backs.
In this Event Method ,You can do the changes in server control also
Unload().
It is the last phase of the page life cycle. . Once this method is completed, the HTML is sent to the browser for client side processing.
we can use this event to cleanup code.
Cleanup can be performed on:
Instances of classes
Closing database connections.
in this phase the page and its controls have been rendered, so you cannot make further changes to the response stream.
protected void Page_UnLoad(object sender, EventArgs e)
.cs page
lblmessage.Text += "Unload <br/>";
because page has been already rendered, so any changes made here will not effect.
Example
.cs page
protected void Page_PreInit(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreInit";
}
protected void Page_Init(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "Init";
}
protected void Page_InitComplete(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "InitComplete";
}
protected override void OnPreLoad(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreLoad";
}
protected void Page_Load(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "Load";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "btnSubmit_Click";
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "LoadComplete";
}
protected override void OnPreRender(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreRender";
}
protected override void OnSaveStateComplete(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "SaveStateComplete";
}
protected void Page_UnLoad(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "UnLoad";
}
output:
After Button Click
Now, i want to disable the ViewState and check the output.
Example
.cs page
protected void Page_PreInit(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreInit";
}
protected void Page_Init(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "Init";
}
protected void Page_InitComplete(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "InitComplete";
}
protected override void OnPreLoad(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreLoad";
}
protected void Page_Load(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "Load";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "btnSubmit_Click";
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "LoadComplete";
}
protected override void OnPreRender(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreRender";
}
protected override void OnSaveStateComplete(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "SaveStateComplete";
}
protected void Page_UnLoad(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "UnLoad";
}
Below is the Order of the Events.
protected void Page_PreInit(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_InitComplete(object sender, EventArgs e)
{
}
protected void Page_PreLoad(object sender, EventArgs e)
{
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
}
protected void Page_PreRender(object sender, EventArgs e)
{
}
Methods | Description |
Page_PreInit | Before page Initialization |
Page_Init | Page Initialization |
LoadViewState | View State Loading |
LoadPostData | Postback Data Processing |
Page_Load | Page Loading |
RaisePostDataChangedEvent | PostBack Change Notification |
RaisePostBackEvent | PostBack Event Handling |
Page_PreRender | Page Pre Rendering Phase |
SaveViewState | View State Saving |
Page_Render | Page Rendering |
Page_Unload | Page Unloading |
PreInit
In this event we can access to master pages and themes.
You can dynamically set the values of master pages and themes in this event.
You can also dynamically create controls in this event.
Note:
Set a master page dynamically.
Set the Theme property dynamically.
protected void Page_PreInit(object sender, EventArgs e)
Example:
Page.MasterPageFile = "~/MasterPage.master";
TextBox masterTextBox = (TextBox)Page.Master.FindControl("txtName");
Label mastLabel = (Label)Page.Master.FindControl("lblErrorMessage");
mastLabel .Text = "Hi, this is Uday";
masterTextBox .Text = "Hi, this is TextBox";
File->Add New Item
MasterPage
Write the Following Code in MasterPage Design as Follows:
Now, Add New Item
Here, I have created a new page Default5 and using the master page dynamically from code behind
Design the Page as follows:
.CS
Now, Run the Program and see the output
Now, my doubt is can i set this master page dynamically in other event.
So, i tried to set this master page in other event after Page_PreInit event, so i am getting an error as
Now, i am copying the same code and pasting in the next event which is
protected void Page_Init(object sender, EventArgs e)
Now, i am getting an error as follows.
Init
This event fires after each control has been initialized.
You can use this event to change initialization values for controls.
we can use this to read the property of each initialized control .
We can Set Unique Id and skin properties of the controls.
We will not get the post back values of the controls.
protected void Page_PreInit(object sender, EventArgs e)
Example:
Suppose you have entered some text in a textbox control and clicking on a button control that makes a postback, now you want to view the value that you have entered. In such case you will be able to see the textbox value which you’ve entered.
.cs page
Output(Runing Mode)
Now, i Enter some text and click on Button
Example 2
.cs Page
OUTPUT:
On Button Click Debugging the Code to see the View State Values
Explanation:
Here on Button Click i am saving the value ViewState["a"]
and when i try to access the value in Page_Init, it does not display the VALUE From ViewState
So in Init we will not get the Post Back Values
InitComplete
Initialization process is completed in this section.
Tracking of View State can be done in this event.
Till now the viewstate values are not yet loaded, We can make changes to ViewState in this event.
protected void Page_InitComplete(object sender, EventArgs e)
PreLoad :
View State data is visible after postback also.
This event will always Fires after the InitComplete Event
From here viewstate functionality starts and we can retrieving their values.
.cs Page
Run the program, and enter any value in text box and click on button click, now, the view state has a value, Now again click on the button and see the output in debugging mode as follows
Clicking on the Button for the second time to view the values in Debugging mode
In PreLoad
In which event controls get loaded
In, Page load event all controls are fully loaded. But Controls can be accessed in Page_Init events but you will see that view state is not fully loaded during this event.
Load
We can validate Page.
You can create dynamic controls in this method.
Controls Events
This event is used to handle control events such as a Button control’s Click event or a TextBox control’s TextChanged event.
If you have a control like button, and you clicked on the Button,so after the Page_Load event your Button_Click event will fire.
Example
Output:
After Button Click
LoadComplete :
The loading process is completed, page validation takes place.
protected void Page_LoadComplete(object sender, EventArgs e)
{
lblmessage.Text += "Page_LoadComplete<br/>";
}
OnPreRender
Here we can have last changes to the page or its control.
This event takes place before saving ViewState, so any changes made here are saved.
For example: After completion of this event , we cannot change any property of a button or change any viewstate value
Use the event to make last changes to the contents of the page or its controls.
Any change will be stored on the page.
protected void Page_PreRender(object sender, EventArgs e)
PreRenderComplete .
this event is the completion of the pre-rendering phase.
SaveStateComplete
The next processed method is SaveViewState(). This method saves the updated Viewstate to be processed on the next page. The final Viewstate is encoded to the _viewstate hidden field on the page during the page render.
Before this event occurs, ViewState has been saved for the page and for all controls.
Any changes to the page or controls at this point will be not done.
If you change the server control then state will not be available in next Post Backs.
In this Event Method ,You can do the changes in server control also
Unload().
It is the last phase of the page life cycle. . Once this method is completed, the HTML is sent to the browser for client side processing.
we can use this event to cleanup code.
Cleanup can be performed on:
Instances of classes
Closing database connections.
in this phase the page and its controls have been rendered, so you cannot make further changes to the response stream.
protected void Page_UnLoad(object sender, EventArgs e)
.cs page
Output
In, the above output i cannot see the value of lblmessage.Text += "Unload <br/>";
because page has been already rendered, so any changes made here will not effect.
Example
.cs page
protected void Page_PreInit(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreInit";
}
protected void Page_Init(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "Init";
}
protected void Page_InitComplete(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "InitComplete";
}
protected override void OnPreLoad(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreLoad";
}
protected void Page_Load(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "Load";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "btnSubmit_Click";
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "LoadComplete";
}
protected override void OnPreRender(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreRender";
}
protected override void OnSaveStateComplete(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "SaveStateComplete";
}
protected void Page_UnLoad(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "UnLoad";
}
output:
After Button Click
Now, i want to disable the ViewState and check the output.
Example
.cs page
protected void Page_PreInit(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreInit";
}
protected void Page_Init(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "Init";
}
protected void Page_InitComplete(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "InitComplete";
}
protected override void OnPreLoad(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreLoad";
}
protected void Page_Load(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "Load";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "btnSubmit_Click";
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "LoadComplete";
}
protected override void OnPreRender(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "PreRender";
}
protected override void OnSaveStateComplete(EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "SaveStateComplete";
}
protected void Page_UnLoad(object sender, EventArgs e)
{
lblmessage.Text = lblmessage.Text + "<br/>" + "UnLoad";
}
Output
After Button Click
Example
.cs Page
protected void Page_PreInit(object sender, EventArgs e)
{
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "PreInit";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_Init(object sender, EventArgs e)
{
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "Init";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_InitComplete(object sender, EventArgs e)
{
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "InitComplete";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
protected override void OnPreLoad(EventArgs e)
{
// Here ,we will always have post back data, in ViewState.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "PreLoad";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_Load(object sender, EventArgs e)
{
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "Load";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "btnSubmit_Click";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "LoadComplete";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
protected override void OnPreRender(EventArgs e)
{
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "PreRender";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
protected override void OnSaveStateComplete(EventArgs e)
{
//Here view state values will not be available during post back.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "SaveStateComplete";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_UnLoad(object sender, EventArgs e)
{
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "UnLoad";
lblmessage.Text = Convert.ToString(ViewState["value"]);
}
Output:
After Button CLick