The Page handler in HTTP Pipe Line is most use than any other Handler and there event’s are also play an important role for creating any application. Think yourself how annoying this is if some times these events are not raised. There fore we must know what are most circumstances that these page events are not work because they are events that build the web application most powerful.
Introduction:
In this Article we see that the three reasons of Page events not work. Although there may be others reasons, but my focus on the reasons which I have seen most. We also see the difference between the Page_Event and standard Control Events like (Page_Init and OnInit), most developer thinks that they are same, but there is some difference which we will see. We also check when the attributes of a server control are Initialize there values which are define in aspx, ascx, etc. We also see how shadowing and AutoEventWireUp is worked.
Description:
Here are three reasons given below,
1) Forgot to call base.onEvent in standard event:
Every aspx Page is actually a composite server control. There fore it has all the events that a server control has. ASP.Net provides standard events within every server control that a developer can catch. Like OnInit, OnLoad, OnDataBinding, OnPreRender etc.
For example you can use OnInit in your code to create dynamic controls as,
The s result of above code is that your Page_Init events never call because you forget to add the base.OnInit(e), because the base implementation of OnInit has necessary code to call these events. Therefore for calling your Page_Event you must call the base.OnEvent method in your override method. If you are not using any override method then base implementation is called which is automatically calling the Page Event.
There is one advantage of using standard Event is that you can call your necessary code before and after these Event’s. For example, you can write your code such that they are executed before and after Page_Load.
I think the above discussion you know that what difference is between
Page_Load, OnLoad, Page_Init and OnInit, and so on
Yes you are right that OnLoad is responsible for calling call the Page_Load etc. One important thing also to note here is that all the server controls attributes which are assigned to before OnInit event. Therefore you can change default values in OnInit easily.
Therefore if you are using any standard event given above then you must call their base implementation in order to call their respective Event.
2) Two implementation of same Event:
Another reason why Page Events are not called is that same Page Event is define both inline (in aspx Page) and in codebehind (in aspx.cs or aspx.vb).
If you have some back ground in OOP then you easily understand what is happens if you have two events in these two files. Let check this with an Example,
Now call the method in your code,
What Method it should call? Parent Method or Child Method?
This is called shadowing. It is always call the Child implementation, but it will also generate a Compiler Warning.
Therefore if you have two implementation of same Events then your Child Method, i.e. Your inline code in aspx page is called. Because your aspx is inherit from your code behind.
Therefore if your code behind Page Events is not calling then also make sure that there is no implementation in aspx file.
3) AutoEventWireUp:
The biggest reason is that your code does use an Event Handler to attach your Page Events and AutoEventWireUp is set to false. For make it work either set AutoEventWireUp to true or attach EventHandler in your code.
One of the confusion about AutoEventWireUp is that most of developer thinks that it is designed for all events. It is not true AutoEventWireUp is only for Page_Event. You can check your self by creating a aspx Page with AutoEventWireup="false” and a Button with OnClick="A".
This will always call A but never call Page_Load. Because Button Handler is attached at run time and Page Event is not automatically attached.
You can find the Handler of A, but no handler of Page_Load in this directory,
WINDOWS\Microsoft.NET\Framework\version\Temporary ASP.NET Files.
This directory contains various files, one of the file contains your Class code and Event Handler as,
Therefore your Page_Event to work you must either set AutoEventWireUp to true or attach this to an Event Handler.
4) Summary:
In this Article we focus on the three important areas where Page_Event are not called and discuss some other important facts like AutoEventWireup, Shadowing, Attributes Initialization and difference between OnEvent and Page_Event.