views:

142

answers:

1

Usually when the page_load event handler is added to a codebehind file by Visual Studio (e.g. double clicking the page in designer mode), it ends up looking like this:

/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void Page_Load(object sender, EventArgs e)
{
    // ...
}

But resharper suggests "Name 'Page_Load' does not match rule 'Methods, properties and events'. Suggested name is 'PageLoad'." I'm guessing there's a better way to define the handler for page load, but I can't remember what the syntax is, but I imagine it would resolve this resharper warning?

Perhaps something like:

/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    // ...
}

but I seem to recall that OnLoad and PageLoad aren't exactly the same?

+2  A: 

OnLoad and Page_Load are essentially the same. Page_Load is invoked via reflection if AutoEventWireup is enabled on the Page. OnLoad is a protected virtual method of the Control class (which the Page class inherits).

See Inside AutoEventWireup for an in-depth analysis of how AutoEventWireup works and why it exists.

The StackOverflow community also has some input on whether it is better to handle the Load event or override the OnLoad method.

Chris Shouts
That's a helpful link with a good explanation. cheers. :)
Andrew Johns

related questions