Page_Load or page top code

D

David C

Can someone tell me if the code in the Page_Load sub runs every time...even
on PostBack? If so, what is the difference between the Page_Load and the <%
%> code at the top of an aspx page? Thanks.

David
 
T

tomisarobot

page load runs every time assuming nothing is broken.

separating code from the ASP/HTML presentation is considered better
organization, which is why you should use your code behind file.

I don't know how the code at the top of the page works in relation to
the event structure to be honest.
 
M

Mark Rae [MVP]

page load runs every time assuming nothing is broken.

Correct, though any code inside Page_Load (and all the other Page_xxx
methods) can be prevented from running at postback by using the IsPostBack
page method...
separating code from the ASP/HTML presentation is considered better
organization, which is why you should use your code behind file.

Not true at all. Inline code or code-behind is simply a matter of personal
perference - it makes not the slightest difference to functionality, or to
the compiler...
 
G

Guest

The OP was asking, "If so, what is the difference between the Page_Load and
the <% %> code at the top of an aspx page?" -- not whether he should be using
codebehind vs. inline code.

The difference is that Page_Load is an intrinsic *event* in the ASP.NET page
lifecycle (there are many others), whereas the <% %> code at the top of an
aspx page is called the Page declaration, which sets up the details of the
page class, what class it uses, and other features that each have attributes
you can set.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top