Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling base Methods When Overriding Page Level Events

In my code behind I wire up my events like so:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    btnUpdateUser.Click += btnUpateUserClick;
}

I've done it this way because that's what I've seen in examples.

  • Does the base.OnInit() method need to be called?
  • Will it be implicitly be called?
  • Is it better to call it at the beginning of the method or at the end?
  • What would be an example where confusion over the base method can get you in trouble?
like image 580
Scott Muc Avatar asked Dec 15 '25 21:12

Scott Muc


1 Answers

I should clarify:

The guidelines recommend that firing an event should involve calling a virtual "OnEventName" method, but they also say that if a derived class overrides that method and forgets to call the base method, the event should still fire.

See the "Important Note" about halfway down this page:

Derived classes that override the protected virtual method are not required to call the base class implementation. The base class must continue to work correctly even if its implementation is not called.

like image 159
Matt Hamilton Avatar answered Dec 18 '25 18:12

Matt Hamilton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!