Page Event handlers in ASP.net 2.0
In ASP.net 2.0, you can declaratively wire an event to a control by adding an attribute/value to your control. The attribute name is the event name and the attribute value is the method to call. VS 2005 will also create the handler for you as shown below.
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e) {
Similarly, since we have a Page_Load event handler in the code behind, I expected to see some kind of declaration in the aspx code but found none.
Thankfully, MSDN explains this.
To create a handler for page events
In the
code editor, create a method with the name
Page_event.
For example, to create a handler for the page's Load event, create a method named Page_Load.
Page event handlers are not required to take
parameters the way that other controls event
handlers are.
ASP.NET pages automatically bind page events to
methods that have the name Page_event. This
automatic binding is configured by the
AutoEventWireup attribute in the @ Page
directive, which is set to true by default. If
you set AutoEventWireup to false, the page does
not automatically search for methods that use
the Page_event naming convention.
http://msdn.microsoft.com/en-us/library/6w2tb12s.aspx
So, if I wanted to handle the Page Init event, I just
need to add this and the event gets bound
automatically.
protected
void
Page_Init() {
Trace.Write("I am in init!");
}
Trace.Write("I am in init!");
}