Share via


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 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 Component Design Guidelines indicate, 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.

Related Topics