14

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?

SteveC
  • 15,808
  • 23
  • 102
  • 173
Andrew Johns
  • 705
  • 1
  • 6
  • 26

3 Answers3

44

Just for completeness I thought I would add here the answer you got from one "David Benson" when you asked on jetbrains.net:

If you go to ReSharper Options, under Languages select C# Naming Style. Select the Advanced settings... button and in the Event subscriptions on fields:, remove the On in front of $event$ so it is $object$_$event$. That should remove the warning.

For me this is the easiest way of placating ReSharper in the face of a large existing web application codebase.

AakashM
  • 62,551
  • 17
  • 151
  • 186
  • 2
    IMHO, this is the correct answer to the question (+1 because it helped me out) – Francesco Gallarotti Jun 15 '11 at 17:11
  • I agree. This is a simpler solution to the problem. Seems like a bug in R# to me that by default, it won't warn if you use Page_OnLoad. – comecme Oct 25 '11 at 18:31
  • beauty, this issue has bugged me for months and I never bothered to look it up. This gets rid of the warnings for Page_Load Page_Init etc, but keeps warnings for all other warnings in tact. Thank you! – Brian Garson Nov 07 '12 at 20:29
7

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.

Community
  • 1
  • 1
Chris Shouts
  • 5,377
  • 2
  • 29
  • 40
0

It's a bug in ReSharper 6.0 and lower. In 6.1 it's fixed. For AutoEventWireup handlers it will not suggest any changes doesn't matter which naming settings you have.

derigel
  • 3,218
  • 2
  • 19
  • 31