views:

53

answers:

2

Hi all. 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?

+1  A: 

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.

<%= Greeting %>

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.

Paul Alan Taylor
@Paul, I agree. And, if we have small and maintainable web applications, any gain in the speed, if attainable, by the in-line coding would be a great win, especially when the application has significant hit rate. Thanks.
sangam
That assumes your apps are always going to be small and maintainable.
Paul Alan Taylor
+1  A: 

Paul is totally right...This will be a nightmare to maintain. Do NOT do this. :|

To answer the original question... No, this will not give you any speed increase. At best, it's break-even. But it could actually create a performance hit. Think about what this code is actually doing on the back end, in the ASP.NET engine and it should be apparent.

Bryan
Yeah, I am curious to learn what would be the actual result. Could you little explain how this would be a performance hit? Thanks.
sangam
More intuition than any solid facts, sorry. :) I believe there is a slight perf hit anytime you switch context in the aspx. But I could be wrong... it's easy to test! From my experience I recall that moving repeater logic from inline code to being handled by OnItemDataBound was a big perf improvement. But that may not be apples-and-apples, I don't remember the exact circumstances.
Bryan
Thanks for the information. I remember I read somewhere that Eval() is not performance friendly. Perhaps that is why moving data binding to codebehind is better. Is there anyway I could exactly measure how this is faster?
sangam
Well, you'd have to code it both ways to really test it. There are any number of ways to measure the raw performance... the easiest would be trace results. Use perfmon counters if you need precise results.
Bryan
Ok, thanks, I will give a try on some time.
sangam