1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

C# pure code behind design

Discussion in 'C#' started by Shaokan, Jan 5, 2011.

  1. #1
    Hi there,

    I've been rummaging through the web but did not come up with anything satisfier. Does anyone have a knowledge whether coding a webpage using only code behind is a good way of creating a website? I mean something as follows:

    The .aspx file is something like

    ...//aspx codes
    <html>
    <body>
    <%=_out %>
    </body>
    </html>

    And the .cs file holds all the code.

    In the page_load event I define the content of the string _out and voila the website is coded.

    For instance
    Page_load event(object sender, event e){

    _out = "this is my webpage, <a href=\"#\">click here</a> if you like it";

    }

    I re-pose my question: Is coding a webpage using only c# like above a valid structure for coding ?
     
    Shaokan, Jan 5, 2011 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You would normally use an <asp:Literal /> rather than <%= %> in the example given

    Certainly using code behind is one of the core benefits of using asp.Net as it allows the programming/ heavy work, to be precompiled into MSIL which is much more efficient than an interpreted scripting language. If you did all your programming in .Net as inline scripting you would hardly be any better off than if you were running classic ASP
     
    AstarothSolutions, Jan 6, 2011 IP
  3. longcall911

    longcall911 Peon

    Messages:
    1,672
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Generally speaking, I would say no. If the page content is only a few words, then it is fine to put that content in an aspnet control. If the content is a typical webpage, wrapping all of it in c# makes it very difficult to work with and to maintain. It is contrary to good programming practices. I would look at it and say "this guy had no idea what he was doing."
     
    longcall911, Jan 9, 2011 IP
  4. webcosmo

    webcosmo Notable Member

    Messages:
    5,840
    Likes Received:
    153
    Best Answers:
    2
    Trophy Points:
    255
    #4
    in some cases programmers use those.
    but in most cases we cant. coding on the code behind page gives lot more flexibility.
     
    webcosmo, Jan 11, 2011 IP
  5. Shaokan

    Shaokan Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I appreciate your answers. I changed the whole structure of my website parallel with your suggestions and I figured that out that I was missing a lot of .net features. Thanks again :)
     
    Shaokan, Jan 23, 2011 IP
  6. MuzD

    MuzD Peon

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You have done the right thing. Happy Coding!
     
    MuzD, Jan 26, 2011 IP
  7. Alnitech

    Alnitech Greenhorn

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #7
    No it is not the right way. HTML or ASP code should be in aspx file. logic should be in .cs file. Javascript should be in .js file or in aspx when you need to use server-generated ids.

    Try to add runat=server attribute and id=myTagId to any of HTML tags and you will be able to access all its properties and events by typing myTagId.<property or event name> in your code behind file.
     
    Alnitech, Feb 14, 2011 IP
  8. roxcon

    roxcon Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    even most of time we use code behind for logic implementation also some of case have to move for those coding both are same and there are no any special advantages or disadvantages but if you always using code behind (.cs) for to write your logic code will be very clean and readable.
     
    roxcon, Feb 14, 2011 IP
  9. trackpaps

    trackpaps Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    if the text you are going for is static and is no dependent by anything else just put it in the aspx/ascx files ... if you side is data driven or uses resource files for multi-language your better using the code behind and creating .net controls.
     
    trackpaps, Feb 16, 2011 IP
  10. prptl709

    prptl709 Guest

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    We can use design part in C# file or not? help me.Thank you
     
    prptl709, Feb 25, 2011 IP
  11. miguelf

    miguelf Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #11
    You can put the design part in C# code but its' not the prefered way of doing it. The design should be in the aspx file and the business logic should be in the C#. It makes page design way more easier.
     
    miguelf, Apr 6, 2011 IP
  12. sd3189541

    sd3189541 Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    What is the use of this asp:Literal ? Please give some idea about that.
     
    sd3189541, Apr 6, 2011 IP
  13. bigmac_lfc

    bigmac_lfc Peon

    Messages:
    131
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I always a simple rule of thumb, if it's design it should be in my aspx or css pages, if it's logic it should be in code behind or business layer
     
    bigmac_lfc, Jun 3, 2011 IP
  14. strike2867

    strike2867 Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #14
    +1 for business layer. -1 for still using WebForms instead of ASP.Net MVC.
     
    strike2867, Jun 5, 2011 IP
  15. salikkhan

    salikkhan Greenhorn

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #15
    I think, you should use the simpler ways of .cs files. Use libraries for the same, it will ease out the coding
     
    salikkhan, Jun 24, 2011 IP