Showing posts with label Event Handler. Show all posts
Showing posts with label Event Handler. Show all posts

Tuesday 8 December 2015

ASP.Net Page Life Cycle

In this post, I will explain you ASP.Net page life cycle & its events.

When an ASP.Net page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. The following are the various stages or events of ASP.Net page life cycle.

PreInit
1.  Check the IsPostBack property to determine whether this is the first time the page is being processed.
2.   Create or re-create dynamic controls.
3.   Set a master page dynamically.
4.   Set the Theme property dynamically.
Note: If the request is a postback then the values of the controls have not yet been restored from the view state. If you set a control property at this stage, its value might be overwritten in the next event.

Init
1.   This event fires after each control has been initialized.
2.   Each control's UniqueID is set and any skin settings have been applied.
3.   Use this event to read or initialize control properties.
4.   The "Init" event is fired first for the bottom-most control in the hierarchy and then fired up the hierarchy until it is fired for the page itself.

InitComplete
1.   Until now the ViewState values are not yet loaded, hence you can use this event to make changes to the view state that you want to ensure are persisted after the next postback.
2.   Raised by the Page object.
3.   Use this event for processing tasks that require all initialization to be complete.

OnPreLoad
1.   Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
2.  Before the Page instance raises this event, it loads view state for itself and all controls and then processes any postback data included with the Request instance.
3.   Loads ViewState: ViewState data are loaded to controls.
4. Loads Postback data: Postback data are now handed to the page controls.

Load
1.   The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.
2.   This is the first place in the page lifecycle that all values are restored.
3.   Most code checks the value of IsPostBack to avoid unnecessarily resetting state.
4.   You may also call Validate and check the value of IsValid in this method.
5.   You can also create dynamic controls in this method.
6.   Use the OnLoad event method to set properties in controls and establish database connections.

Control PostBack Event(s)
1.    ASP.NET now calls any events on the page or its controls that caused the PostBack to occur.
2.    Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.
3.    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.
4.    This is just an example of a control event. Here it is the button click event that caused the postback.

LoadComplete
1.   Raised at the end of the event-handling stage.
2.   Use this event for tasks that require that all other controls on the page be loaded.

OnPreRender
1.   Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls.
2.   The Page object raises the PreRender event on the Page object, and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page.
3.   The PreRender event of individual controls occurs after the PreRender event of the page.
4.   Allows final changes to the page or its control.
5.   This event takes place before saving ViewState, so any changes made here are saved.
6.   For example: After this event, you cannot change any property of a button or change any ViewState value.
7.   Each data-bound control whose DataSourceID property is set calls its DataBind method.
8.   Use the event to make final changes to the contents of the page or its controls.

OnSaveStateComplete
1.   Raised after view state and control state have been saved for the page and for all controls.
2.   Before this event occurs, ViewState has been saved for the page and for all controls.
3.   Any changes to the page or controls at this point will be ignored.
4.   Use this event to perform tasks that require the view state to be saved, but that do not make any changes to controls.

Render
1.   This is a method of the page object and its controls (and not an event).
2.   The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and a script that are necessary to properly display a control at the browser.

UnLoad
1. This event is used for cleanup code.
2. At this point, all processing has occurred and it is safe to dispose of any remaining objects, including the Page object.
3. Cleanup can be performed on:
o   Instances of classes, in other words, objects
o   Closing opened files
o   Closing database connections.
4. This event occurs for each control and then for the page.
5. During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream.
6. If you attempt to call a method such as the Response.Write method then the page will throw an exception.

Monday 7 December 2015

Global.asax file in ASP.Net

In this post, I will explain to you about Global.asax file and Events in the Global.asax file.
A Global.asax file (also known as the ASP.NET application file) is an optional file that is located in the application's root directory and is the ASP.NET counterpart of the Global.asax of ASP. This file exposes the application and session-level events in ASP.NET and provides a gateway to all the application and the session-level events in ASP.NET. This file can be used to implement the important application and session-level events such as Application_Start, Application_End, Session_Start, Session_End, etc. This article provides an overview of the Global.asax file, the events stored in this file and how we can perform application-wide tasks with the help of this file.

What is the Global.asax file?

According to MSDN, "The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET." The Global.asax file is parsed and dynamically compiled by ASP.NET into a .NET Framework class the first time any resource or URL within its application namespace is activated or requested. Whenever the application is requested for the first time, the Global.asax file is parsed and compiled to a class that extends the HttpApplication class. When the Global.asax file changes, the framework reboots the application and the Application_OnStart event is fired once again when the next request comes in. Note that the Global.asax file does not need recompilation if no changes have been made to it. There can be only one Global.asax file per application and it should be located in the application's root directory only.

Events in the Global.asax file
The following are some of the important events in the Global.asax file.
  • Application_Init
  • Application_Start
  • Session_Start
  • Application_BeginRequest
  • Application_EndRequest
  • Application_AuthenticateRequest
  • Application_Error
  • Session_End
  • Application_End
The purpose of these event handlers is discussed in this section below.
Application_Init
The Application_Init event is fired when an application initializes the first time.
Application_Start
The Application_Start event is fired the first time when an application starts.
Session_Start
The Session_Start event is fired the first time when a user’s session is started. This typically contains for session initialization logic code.
Application_BeginRequest
The Application_BeginRequest event is fired each time a new request comes in.
Application_EndRequest
The Application_EndRequest event is fired when the application terminates.
Application_AuthenticateRequest
The Application_AuthenticateRequest event indicates that a request is ready to be authenticated. If you are using Forms Authentication, this event can be used to check for the user's roles and rights.
Application_Error
The Application_Error event is fired when an unhandled error occurs within the application.
Session_End
The Session_End Event is fired whenever a single user Session ends or times out.
Application_End
The Application_End event is the last event of its kind that is fired when the application ends or times out. It typically contains application cleanup logic.

The Global.asax file is used in ASP.NET to specify the global objects and the application and the session-level events that would be used by the application. It contains all the application and session-level events that are used by the application.