Here is my 2 cents on the subject to reiterate what Hal et al have previously state but also to provide some clarification on why inline code pages do not require "Handles":
Page classes
Inline code: When an ASPX page is processed by the ASP.NET runtime, the runtime dynamically generates a class named like <pagename>_aspx. This class is derived from System.Web.UI.Page. All the code in the inline code (<script runat="server">) is part of this dynamic class.
Code-behind: The @ Page directive specifies the class that the ASPX page will be derived from (Inherits="webapplication.myPage"). This class exists in the .NET project as nothing more than another class that is derived from System.Web.UI.Page. It just happens to be associated to the ASPX file within the development environment as the "code-behind" file and is compiled into the DLL.
An ASP page class has the property "AutoEventWireup". When this property is true, the ASP.NET runtime attempts to locate a predefined set of method in the class that the page is derived from. "Page_Load" is an example of this. The runtime automatically wires up the System.Web.UI.Page.Load event to this method so that it gets called when the event is fired. This is how you can have a function called Page_Load in the inline code without the "Handles" keyword (in
VB.NET).
When working with the code-behind model in
VB.NET, you normally has the "Handles" keyword on methods that need to be wired to page events. This performs the event wiring.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
'Put user code to initialize the page here
End Sub
In C# it's done differently by means of programmatically adding EventHandler objects to the event in the InitializeComponent() method that gets called by the OnInit() method. The ASP.NET runtime calls OnInit() when it starts processing the page. Here's the C# code:
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
The reason you need to programmatically wire the events (in either language) is because when a new web form is created the AutoEventWireup property is turned off in the @ Page directive (AutoEventWireup="False"). Without this setting being true you MUST programmatically wire the events.
I did a small test to satisfy my curiosity about these inner-workings and different coding models.
Here's the ASPX page EventWireup.aspx:
<%@ Page Language="
vb" AutoEventWireup="true" src="EventWireup.aspx.
vb" Inherits="CEventWireup"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) ' Handles MyBase.Load
'Put user code to initialize the page here
Label1.Text = "This is text set in Page_Load()."
End Sub
</script>
<title>Event Wireup</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:label id="Label1" runat="server">This is the default Label1 text in the markup HTML.</asp:label><br>
<asp:label id="Label2" runat="server">This is the default Label2 text in the markup HTML.</asp:label><br>
</form>
</body>
</html>
And here's the code-behind file EventWireup.aspx.
vb:
Public Class CEventWireup
Inherits System.Web.UI.Page
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Private Sub Page_Load2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Label2.Text = "This is text set in Page_Load2()."
End Sub
End Class
Notice that I set AutoEventWireup="true". This was so I could see that both of my Page_Load methods got called. One with "Handles" commented out but with the default name of "Page_Load" so it would get picked up by the auto wiring. The other I named different, but left the "Handles" keyword on it. The resulting output is:
This is text set in Page_Load().
This is text set in Page_Load2().
The key is to understand when methods are getting automatically wired to the events and when they need to be programmatically wired.
Peter
-------------------------
Work smarter, not harder