inline code VS page behind code

Discussion in 'ASP .Net' started by Mitch A, Nov 1, 2003.

  1. Mitch A

    Mitch A Guest

    What are the advantages of using inline C# code in my aspx pages (with
    inline I mean directly in the html between <% %>) versus code behind. It
    seems messy and inelegant to have code in both places (IMHO, using inline
    *and* code-behind is not object oriented and it generates less readable
    code). Since code behind offers syntax highlighting, autocompletion, member
    info, parameterinfo etc, is there any reason (design or other) I
    couldn't/shouldn't write all code strictly using code-behind.
     
    Mitch A, Nov 1, 2003
    #1
    1. Advertising

  2. "Mitch A" <> wrote in
    news:3fa41808$:

    > What are the advantages of using inline C# code in my aspx pages
    > (with inline I mean directly in the html between <% %>) versus
    > code behind. It seems messy and inelegant to have code in both
    > places (IMHO, using inline *and* code-behind is not object
    > oriented and it generates less readable code). Since code
    > behind offers syntax highlighting, autocompletion, member info,
    > parameterinfo etc, is there any reason (design or other) I
    > couldn't/shouldn't write all code strictly using code-behind.


    Mitch,

    I can't think of any. I use code-behind as much as possible. I only
    use inline <%= %> tags if I need to insert some text at runtime in a
    place where ASP.Net tags can't be used (or at least not easily used).

    The <HEAD> section of an HTML page is a good example. What few
    inline tags I do use I keep very short, and any actual calculations
    are done in code-behind:

    <HEAD>
    <TITLE>
    <%= Globals.Instance.GetCompanyName() %>
    </TITLE>
    <SCRIPT type="text/javascript"
    src="../../scripts/clipboard.js">
    </SCRIPT>
    <%= Globals.Instance.GetCommentHeader() %>
    <META name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    <%= Globals.Instance.GetMetaTags() %>
    <LINK rel="stylesheet" type="text/css" href="../../portal.css">
    </HEAD>

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
     
    Chris R. Timmons, Nov 1, 2003
    #2
    1. Advertising

  3. Mitch A

    Mitch A Guest

    Thanks for responding Chris. Is it possible to manipulate ASPNET server
    controls using only codebehind? For example, I have the following bit of
    code currently embdedded in the HTML:

    <asp:Repeater id="parentRepeater" runat="server">
    <itemtemplate>
    <table cellpadding=0 cellspacing=0 border=0 width=80% >
    <td class="CandInfo"width="100%">
    <%# DataBinder.Eval(Container.DataItem, "NM_FRST") %>
    <td> </table>
    </itemtemplate>
    </asp:Repeater>


    Other than the HTML tags identifying the control (<asp:Repeater>), can any
    of this code be moved to the webform Page_Load event?
     
    Mitch A, Nov 1, 2003
    #3
  4. "Mitch A" <> wrote in
    news:3fa43f73$:

    > Thanks for responding Chris. Is it possible to manipulate
    > ASPNET server controls using only codebehind? For example, I
    > have the following bit of code currently embdedded in the HTML:
    >
    ><asp:Repeater id="parentRepeater" runat="server">
    > <itemtemplate>
    > <table cellpadding=0 cellspacing=0 border=0 width=80% >
    > <td class="CandInfo"width="100%">
    > <%# DataBinder.Eval(Container.DataItem, "NM_FRST") %>
    > <td> </table>
    > </itemtemplate>
    ></asp:Repeater>
    >
    >
    > Other than the HTML tags identifying the control
    > (<asp:Repeater>), can any of this code be moved to the webform
    > Page_Load event?


    Mitch,

    I suppose it's technically possible to do so (see the help entry for
    the System.Web.UI.WebControls.Repeater class). Personally, I don't
    create controls in code unless I absolutely have to. I find it
    easier to use the property editor to manipulate the control, and use
    code-behind for everything else. The less code I have to actually
    write, debug and maintain, the happier I am :).

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
     
    Chris R. Timmons, Nov 2, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    589
    Alphonse Giambrone
    Jun 28, 2003
  2. Anders M

    Using inline-code with code behind.

    Anders M, Apr 28, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    7,455
    Karl Seguin
    Apr 28, 2005
  3. Kamal Ahmed
    Replies:
    1
    Views:
    5,212
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
    Oct 25, 2005
  4. Replies:
    8
    Views:
    705
    Kevin Spencer
    Dec 9, 2005
  5. John Dalberg

    Tool to move inline code to code behind?

    John Dalberg, Mar 20, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    323
    Alvin Bruney [MVP]
    Mar 22, 2007
Loading...

Share This Page