Using Page-Level Event Methods

You can implement two page-level event methods, OnStartPage and OnEndPage, in your Web component (object) to perform tasks before and after the actual ASP page runs. These methods are optional. ASP pages will work with any automation object, regardless of whether these methods are implemented.

When an object is called from an ASP page, the Web server creates an object instance and looks for the OnStartPage and looks for the OnStartPage and OnEndPage methods associated with the object's IDispatch interface. If the object has implemented these methods, the Web server automatically calls the OnStartPage method during script processing before the object is used and calls the OnEndPage method after all scripts in the ASP page have been processed.

The following table illustrates the way an ASP page must call an object in order for the page-level event methods to be called.

Object created with the following code in an ASP page

OnStartPage is called in the object

OnEndPage is called in the object

<OBJECT runat=Server scope=Application ...>

Never

Never

<OBJECT runat=Server scope=Session ...>

Never

Never

Application("variable_name") = Server.CreateObject("progid")

When the Web application loads

When the Web application unloads

Session("variable_name") = Server.CreateObject("progid")

When the user session starts

When the user session ends

variable_name = Server.CreateObject("progid")

When the object is created in the ASP page

When the ASP page finishes running

variable_name = CreateObject("progid")

Never

Never

As the topic Setting the Scope of COM Objects in ASP Pages indicates, very few objects should be given application scope, whether by using the <OBJECT> tag or the Web server's Application object. Good programming practice calls for your object, regardless of scope, to handle the situation when the page-level event methods fail or are not called. For example, in an ASP page, if you or another developer accidentally creates an object using the CreateObject method instead of the Server.CreateObject method, you may expect that the page-level event methods are running when they are not.

At the start or end of an application or session, you can have code run in an object, as described above, or you can have code run in a global.asa file by using the application and session events.

OnStartPage

When a user requests a Web page in an ASP application, the server calls the OnStartPage method for all objects attached to that page, session, or application. This call occurs one time.

Objects created with the <OBJECT> tag never receive a call for the OnStartPage method.

If you want to access the ASP built-in objects from your OnStartPage method, use the COM+ ObjectContext object.

OnEndPage

After the server finishes processing script on an ASP page, it calls the OnEndPage method for all objects attached to that page, session, or application. This call occurs one time.

Objects created with the <OBJECT> tag never receive a call for the OnStartPage method.

You must use the OnEndPage method to free copies of objects created in the OnStartPage method and any other resources that are not necessary after the server finished processing the script.

See Also