what's the differences between "Overrides Sub OnError" and "Sub Page_Error" ?

D

Derek Harmon

Tee said:
what's the differences between "Overrides Sub OnError" and "Sub Page_Error"

Overrides Sub OnError is:

1) faster.
2) does not require you to write a Handles Page.Error or AddHandler AddressOf( Me.Page_Error).

However, when overriding OnError, you must begin your Sub with:

MyBase.OnError( eventArgs)

Here's the low-down. The ASP.NET Framework will call your Page's (or any ControlTemplate's)
OnError( ) method when an unhandled exception occurs. By default, the definition of OnError in
your base class will go through all of the EventHandlers on the Page.Error event (i.e., if you added
a Handles clause, AddHandler statement, or AutoEventWireup="True", your Page_Error sub would
be called now).

Since the Framework automatically calls OnError( ) at the appropriate time, and you have subclassed
the Page (so you have access to override it, because it is Protected), why go through all the overhead
of hooking up an event handler? That's why it's faster. But you must remember to call your base's
OnError( ) if you do it this way, otherwise event handlers that have been hooked-up won't be fired.

If your code lives outside of the web application's Page subclass, then you cannot override OnError( ),
and the only way to tap the Page.Error event is to handle it via an AddHandler statement from within
your external class.


Derek Harmon
 
T

Tee

Very nice explanation, Thank you.


Derek Harmon said:
Page_Error"

Overrides Sub OnError is:

1) faster.
2) does not require you to write a Handles Page.Error or AddHandler AddressOf( Me.Page_Error).

However, when overriding OnError, you must begin your Sub with:

MyBase.OnError( eventArgs)

Here's the low-down. The ASP.NET Framework will call your Page's (or any ControlTemplate's)
OnError( ) method when an unhandled exception occurs. By default, the definition of OnError in
your base class will go through all of the EventHandlers on the Page.Error event (i.e., if you added
a Handles clause, AddHandler statement, or AutoEventWireup="True", your Page_Error sub would
be called now).

Since the Framework automatically calls OnError( ) at the appropriate time, and you have subclassed
the Page (so you have access to override it, because it is Protected), why go through all the overhead
of hooking up an event handler? That's why it's faster. But you must remember to call your base's
OnError( ) if you do it this way, otherwise event handlers that have been hooked-up won't be fired.

If your code lives outside of the web application's Page subclass, then
you cannot override OnError( ),
and the only way to tap the Page.Error event is to handle it via an
AddHandler statement from within
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top