In ASP.NET, do inline expressions get executed before or after the code behind?
Asked Answered
S

1

2

Do ASP inline expressions <% ... %> get run on the server before or after the code in the code-behind is executed?

Sherrisherrie answered 4/3, 2013 at 19:28 Comment(2)
I believe your question was already answered here https://mcmap.net/q/871125/-asp-net-inline-codeLeukorrhea
Thanks Marcus, but I don't see where it specifically says in that post whether the inline expressions excute before or after the code behind. In other words, one can depend on the other already being executed, but which way is it?Sherrisherrie
I
7

Inline code executes after prerender page in the asp.net page life cycle. Because of this there are certain things that it can run before and certain things it can run after. Read up on the page life cycle here:

http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

The events that come after prerender are:

  • PreRenderComplete
  • SaveStateComplete
  • Render
  • Unload

So any code in the other page lifecycle events will execute first.

From Microsoft's documentation:

An embedded code block is server code that executes during the page's render phase.

Inversely answered 4/3, 2013 at 19:32 Comment(8)
Thanks Abe, but I already read that page prior to my post and I couldn't determine the order of the two. It doesn't specifically mention either of the two so I was left wondering. Could you shed some light on that? Can my inline expressions rely on code behind variables to be already be initialized? or would it be the other way around?Sherrisherrie
@Sherrisherrie with simple words, is executed after the code behind.Internode
@johntrepreneur, I went ahead and added a link to Microsoft's documentation where they go over when that code will execute. I am pretty sure you can rely on code behind variables as long as they are initialized before the prerender event. They will probably have to be public.Inversely
@Aristos, I do not think that is true. The 4 events that I specified can have their own code in the code behind file. "Code behind" isn't just one unit of code. There are several events that make it up, all executing in a specific order.Inversely
@AbeMiessler This events are the part of the page render. So its logical that at least one event is needed to render that code, but the idea is that execute after the code behind. This events is the last stage of the final render of the page.Internode
Hrrm, I'm not sure I follow. How can the code for events associated with rendering execute after the code behind, when they are part of the code behind themselves?Inversely
So after some testing referencing public variables in the code behind that are set in the .page_load method, it appears that they are available and have been set for the inline expressions to reference and that the .page_load has already been executed.Sherrisherrie
Abe, you are right about code behind consisting of several events. I should have been more specific saying that my question is in relation to the .page_load method.Sherrisherrie

© 2022 - 2024 — McMap. All rights reserved.