I have a query that hit my mind when I was adding an asp.net web page in a project. Normally we place the server side code in the codebehind file. Could there be any improvement in the server side processing if we place both the code and the page design markup in the same page? I am referring to the practice like this:
<div>
<%if (ViewState["user-id"] != null)
{%>
<div>Hi, You are welcome.</div>
<%}
else
{%>
<div>Hi, please login or register.</div>
<%} %>
</div>
If we use separate codebehind file, we would do all these in the page load event and make div elements visible, invisible according to the test. We could even have only one div in the design page and set its inner text accordingly. Any suggestions?
Interesting question.
If this runs any faster, you’re going to have to offset this against the cost of maintenance on your pages. This style of coding was commonplace during the Classic ASP era. The markup can be a total nightmare to maintain, particularly if your coding team and design team are different people.
My suggestion? Only use inline stuff when you want to spit out a value in tie-fighter syntax.
Use the codebehind for anything that involves control or loop structures. I’d genuinely be interested in whether in-line is faster than codebehind, but any speed increase you might gain is going to be downright negligible compared to the hassle you’ll have maintaining software built using this approach.