asp.net - Multiple Page_Load methods, which is called? -
in existing asp.net application, have base class containing page_load method:
public class pagebaseclass : system.web.ui.page { protected virtual void page_load(object sender, eventargs e) { // stuff... } } i have actual page inherits base class. however, doesn't override existing page_load method, declares new 1 this:
public class actualpage : pagebaseclass { protected void page_load(object sender, eventargs e) { // other stuff... } } the compiler gives me warning page_load method in actual page hiding existing page_load method. effectively, there 2 seperate page_load methods, since old 1 wasn't overridden, hidden.
now question is, asp.net architecture in it's lifecycle in such situation? 1 being called? or both being called?
note: know bad design, i'm not sure original author had in mind, i'm trying understand what's happening , how affect logic of system.
after digging around, i've found out what's happening.
short answer: top-most method called!
at first put down breakpoints melanciauk suggested in comment. showed me top-most method being called.
digging deeper, looked @ volpav suggested. true autoeventwireup set true, automatically hooking method event handler (since indeed wasn't done manually). however, unlike claimed, top page_load method being called.
reflecting on type gave me bit of clue going on:
var pageloads = this.gettype().getmethods(system.reflection.bindingflags.instance | system.reflection.bindingflags.nonpublic).where(m => m.name == "page_load"); var pageload = this.gettype().getmethod("page_load", system.reflection.bindingflags.instance | system.reflection.bindingflags.nonpublic); the pageloads value ended containing 2 results: 2 page_load methods, 1 declaring type actualpage , 1 declaring type pagebaseclass.
i though cause problems second call, since having 2 results, expected throw ambiguousmatchexception. however, did not. contained methodinfo method in actualpage.
digging deeper, stumbled upon article k. scott allen: inside autoeventwireup http://odetocode.com/blogs/scott/archive/2006/02/16/inside-autoeventwireup.aspx
according article, wiring events happening follows:
delegate.createdelegate(typeof(eventhandler), this, "page_load", true, false); executing myself, found call creates delegate pointing @ page_load method actualpage, same way getmethod call returned method.
so seems in case event-method being wired automatically, not wire methods being hidden.
Comments
Post a Comment