C# Code in Markup-Script-Tag or Codebehind

  1. #1

    C# Code in Markup-Script-Tag or Codebehind

    Hello,

    I just found it, that I do not need to recompile my whole solution,
    when I change C# Code inside a <script runat="server">-Tag in Markup.

    This only takes seconds to test something instead of 2-3 Minutes for writing the code in codebehind,
    stopping the debugger, restarting the debugger.


    Does anybody of you know any disadvantages of writing code in markup?

    For example that the Customer can see the sourcecode.

    Or is it slower on runtime if the code is in the markup file or... whatever?



    Regards,

    Martin
  2. #2

    RE: C# Code in Markup-Script-Tag or Codebehind

    First big thing that comes to mind is error checking. If you put your code in the page and not the page behind you lose compile time checking. For Example:

    
            public void foo()
            {
                object x = 1;
                x = x as int;
            }
    this code in page behind will not be compiled till the method is called while if you put this code in page behind it is compiled up front and you will see the error right away. If you deploy this code and it is not used for months the error will sit there until someone finally uses the page and it breaks.
  3. #3

    RE: C# Code in Markup-Script-Tag or Codebehind

    Hello macap,

    We call that inline code blocks, they will not show to the public but they do have a couple cons:


    1. Performance; since these are not compiled into an assembly the JIT compiler will have to handle these. If you are using a lot of inline code blocks (for instance loading lots of user controls) you will notice a degradation in your response time


    2. Error checking; again since you are not compiling you will not be able to notice some compilation errors until you load the page


    Our rule of thumb at my work is to avoid using inline code blocks where ever possible. It is Microsoft best practice to compile your applications for optimal performance.


    Cheers,
    Timothy
  4. #4

    RE: C# Code in Markup-Script-Tag or Codebehind

    To add to Timothy's post:

    3. Organization and Maintenance: it is easier to maintain a code behind file then putting it onto the aspx page. With inline code blocks, you may fall back into bad habits of spaghetti code in classic ASP.


    Inline code blocks is great for testing and messing around. I wouldn't do it in production though. For example, I have my own Coolite Sandbox web project where I test new features and create sample code for bug reporting. It is much more helpful to post a complete code sample of one aspx file with both server and client code than to post them separately.
  5. #5

    RE: C# Code in Markup-Script-Tag or Codebehind

    Ok. Thanks to your comments!

    So I guess the best is to only use Codebehind but use Inline-Code for solving current bugs
    to avoid compile-time until a proper solution is found.



    Regards,

    Martin
  6. #6

    RE: C# Code in Markup-Script-Tag or Codebehind

    Please report it to mono bug list. The person responsible for this is Marek Habershack. And he is

Similar Threads

  1. [CLOSED] [1.0]Convert markup DirectEvent to codebehind
    By betamax in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: May 19, 2010, 11:31 AM
  2. Replies: 6
    Last Post: Apr 30, 2010, 2:57 PM
  3. Why use Markup instead of Codebehind?
    By dbassett74 in forum 1.x Help
    Replies: 1
    Last Post: Apr 21, 2009, 7:51 PM
  4. Replies: 3
    Last Post: Apr 21, 2009, 6:19 PM
  5. Prb: Moving objects from codebehind til markup
    By plykkegaard in forum 1.x Help
    Replies: 1
    Last Post: Mar 22, 2009, 2:16 PM

Posting Permissions