Hi,
I've been working in ASP.Net for only a short few months now. In my efforts
to understand the execution order of the various built-in routines on a web
form, I have spent a little time running the debugger. Something very
strange seems to be happening. I'm finding that several routines execute
twice. I have gone thru all logical explanations that I can come up with
for why I'd see this.
My scenario is this: I have an aspx file on which is placed a single
occurrence of a user control which comes from a separate ascx file. Inside
this user control is where I'm seeing the duplicate executions. I placed a
breakpoint on the declaration of every function inside my user control class
to see how things happen. Here's an example of the execution order I found
during the first call to the page:
New()
OnInit()
Page_Init()
InitializeComponent()
Page_Init()
InitializeComponent()
Page_Load()
Page_Load()
CreateChildControls()
onPreRender()
Render()
A postback execution seems to make no difference aside from calling the
postback event (button click, etc.). Now the first thing I noticed is that
Page_Init executes twice. (InitializeComponent is called from within
Page_Init so that's fine.) The Page_Init function declaration contains
"Handles MyBase.Init". What does this mean? Is something happening where
this function gets called once for this control's base control (which is??)
init event and a second time for this control's init event? Similarly, the
Page_Load declaration contains "Handles MyBase.Load". Is the same
possibility true for this guy as well? If this is the case, can someone
explain why this is set up like this?
Something else I noticed. The sequence above was found by hitting F5 at
each breakpoint. I tried again using F10 "Step Over" and got something new.
I saw Finalize() come up, which I should have seen anyway cause there's a
breakpoint on the function declaration. It also seems like that popped up
DURING the execution of CreateChildControls. There doesn't seem to be
consistency to the behavior of the debugger. Perhaps that's all it is,
debugger quirks, but either way, some routines are executing twice and it's
causing very strange results.
The only remote explanation I have concocted is that the page "loads" once
for postback, then again for rendering. It makes little sense, and further
tests have led me to believe that this is not the case (trace output of
IsPostBack in the page_load function returns the same value across the
redundant executions).
I'd greatly appreciate some information on how this is all working. I'm
very very baffled by what I am seeing.
Peter