Within each stage of the life cycle of a page, the page raises events that you can handle to run your own code. For control events, you bind the event handler to the event, either declaratively using attributes such as OnClick, or in code.
Pages also support automatic event wire-up, meaning that ASP.NET looks for methods with particular names and automatically runs those methods when certain events are raised. If the AutoEventWireup attribute of the @ Page directive is set to true (or if it is not defined, since by default it is true), page events are automatically bound to methods that use the naming convention of Page_Event, such as Page_Load and Page_Init.
The following table lists the page life-cycle events that you will use most frequently. There are more events than those listed; however, they are not used for most page processing scenarios. Instead, they are primarily used by server controls on the ASP.NET Web page to initialize and render themselves.
Following are the page life cycle events:
PreInit
PreInit is the first event in page life cycle.
We use this event for the following:
Check the IsPostBack property to determine whether this is the first time the page is being processed.
Create or re-create dynamic controls.
Set a master page dynamically.
Set the Theme property dynamically.
This event can be handled by overriding the OnPreInit method or creating a Page_PreInit handler as following:
protected override void OnPreInit(EventArgs e) {
base.OnPreInit(e);
} OR
protected void Page_PreInit(object sender, EventArgs e) {
}
Init
Raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.
This event can be handled by overriding the OnInit method or creating a Page_Init handler as following:
protected override void OnInit(EventArgs e) {
base.OnInit(e);
} OR
protected void Page_Init(object sender, EventArgs e) {
}
InitComplete
Raised by the Page object. Use this event for processing tasks that require all initialization be complete.
This event can be handled by overriding the OnInitComplete method or creating a Page_InitComplete handler as following:
protected override void OnInitComplete(EventArgs e) {
base.OnInitComplete(e);
} OR
protected void Page_InitComplete (object sender, EventArgs e) {
}
PreLoad
Use this event if you need to perform processing on your page or control before the Load event.
After the Page raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.
This event can be handled by overriding the OnPreLoad method or creating a Page_PreLoad handler as following:
protected override void OnPreLoad(EventArgs e) {
base.OnPreLoad (e);
} OR
protected void Page_PreLoad (object sender, EventArgs e) {
}
Load
The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded.
Use the OnLoad event method to set properties in controls and establish database connections.
This event can be handled by overriding the OnLoad method or creating a Page_Load handler as following:
protected override void OnLoad(EventArgs e) {
base.OnLoad (e);
} OR
protected void Page_Load (object sender, EventArgs e) {
}
LoadComplete
Use this event for tasks that require that all other controls on the page be loaded.
The loading process is completed, control event handlers are run, and page validation takes place.
This event can be handled by overriding the OnLoadComplete method or creating a Page_LoadComplete handler as following:
protected override void OnLoadComplete (EventArgs e) {
base.OnLoadComplete (e);
OR
protected void Page_LoadComplete (object sender, EventArgs e) {
}
PostBack Events (Control Events)
Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.
Note:
In a postback request, if the page contains validator controls, check the IsValid property of the Page and of individual validation controls before performing any processing.
For Example: Button Control’s Click Event
protected void Button1_Click(object sender, EventArgs e) {
}
PreRender
The PreRender event occurs just before the output is rendered. By handling this event, pages and controls can perform any updates before the output is rendered.
OR
Use this event to perform any updates before the server control is rendered to the page. Any changes in the view state of the server control can be saved during this event. Such changes made in the rendering phase will not be saved.
The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.
This event can be handled by overriding the OnPreRender method or creating a Page_ PreRender handler as following:
protected override void OnPreRender (EventArgs e) {
base.OnPreRender (e);
} OR
protected void Page_PreRender (object sender, EventArgs e) {
}
PreRenderComplete
As the PreRender event is recursively fired for all child controls, this event ensures the completion of the pre-rendering phase.
OR
The PreRenderComplete event is raised when the pre-render stage of the page life cycle is complete.
At this stage of the page life cycle, the page is ready to render to the output.
This is the last event raised before the page's view state is saved.
This event can be handled by overriding the OnPreRenderComplete method or creating a Page_
PreRenderComplete handler as following:
protected override void OnPreRenderComplete (EventArgs e) {
base.OnPreRenderComplete (e);
} OR
protected void Page_PreRenderComplete (object sender, EventArgs e) {
}
SaveStateComplete
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 ignored.
Use this event perform tasks that require view state to be saved, but that do not make any changes to controls.
This event can be handled by overriding the OnSaveStateComplete method or creating a Page_
SaveStateComplete handler as following:
protected override void OnSaveStateComplete (EventArgs e) {
base.OnSaveStateComplete (e);
} OR
protected void Page_SaveStateComplete (object sender, EventArgs e) {
}
Render
Render is not an event but this is the phase where the HTML of your page will get rendered to the output stream with the help of HTMLTextWriter. You can override the Render method of page if you want to write your own code rather than the actual HTML text. As like below:
protected override void Render(HtmlTextWriter writer) {
base.Render(writer);
Response.Write("<h1>Hello ASP.NET</h1>");
}
UnLoad
This is the last event that gets fired. This is the page cleaning process like closing the open file connections etc., so in this process you cannot do any kind of manipulation with data that affect the rendering, you are restricted to use Response object also, and doing such you will get an exception message.
Clean-up can be performed on following:- (a) Instances of classes i.e. objects (b) Closing opened files
(c) Closing database connections.
This event can be handled by overriding the OnUnload method or creating a Page_ Unoad handler as following:
protected override void OnUnload (EventArgs e) {
base.OnUnload (e);
} OR
protected void Page_Unload (object sender, EventArgs e) {
}