Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. A question of style: Inline code in .aspx pages.

A question of style: Inline code in .aspx pages.

Scheduled Pinned Locked Moved The Lounge
htmlasp-netarchitecturequestioncsharp
34 Posts 25 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Vark111

    Situation: based on the contents of some model property, you have to display a particular DIV as one css class or another. So how do you solve this *without* mixing markup and code? Regardless if you're using MVC or not? Either your model has to have CSS class string constants embeded internally (BAD) or you have to branch in your markup (BAD). So which would you do? Me? I choose to branch in my markup. Deciding which css class to use is very definitely a display concern, so it belongs in the view/aspx/cshtml/whatever.

    B Offline
    B Offline
    Brady Kelly
    wrote on last edited by
    #25

    Vark111 wrote:

    Situation: based on the contents of some model property, you have to display a particular DIV as one css class or another.

    I render the model property as JavaScript and use jQuery to conditionally set the class of the DIV.

    1 Reply Last reply
    0
    • C Chris Maunder

      Back in the dark ages when we wrote ASP pages, our only real choice was to inter-mingle our code and our HTML. We'd write a little HTML, then open a script block with <%, then emit some data, or start a loop, or open a connection to a database or whatever. Then we'd close the block and continue on with HTML. Then came ASP.NET and WebForms and suddenly the code-behind (and then code-beside) model dictated that there was a separation of code and markup. You were meant to be able to scatter your server-side controls onto a page like any other markup, code against it in the code-behind, then send just the .aspx page with the markup to a designer who would make it perfect and who would never be able to break you code, since there was no code on the page. Then came MVC and suddenly we're back to inline code. Well, not necessarily code - more inline binding of the properties of the view-model. Except where you need to loop. And maybe that bit where you need to branch. And maybe that other bit, too. The whole argument for the clean, precise separation of code and markup seems to never be mentioned these days and so I'd like to know how ASP.NET developers feel about this. Regardless of what framework you use, do you mix it up like it's 1999? Do you stick to your guns and keep Church and State separated? Or do you embrace the inline binding of view-model to view and just not get carried away too much. If possible?

      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

      M Offline
      M Offline
      Michael A Cochran
      wrote on last edited by
      #26

      Personally, I hate inline code and it really has little to do with separation of concerns. It's bitten my butt several times over the years as you try to do things like set page themes or dynamically inject controls in the page processing pipeline. I avoid it like the plague. My recipe for an ASP.Net WebForms aplication... Use jQuery and widgitize or plugin the bejesus out of everything that is even remotely reusable. There should be very few server-side dependencies here as each widget or plugin is highly independent and provides very targeted functionality that serves to extend functionality of the markup in the client; e.g., a datatable widget that adds paging to an HTML table element. Almost everything on the UI is broken into UserControl style reusable blocks. Each UserControl has a javascript controller to manage interactions on the client. Minimize server dependencies in the client controller to AJAX calls to a REST bus to acquire data as needed. Where it makes sense, data is returned as highly semantic markup; e.g., an HTML table element. When you return semantic markup like this, you're not really returning UI (view), you're really returning data (model). On the server side, make them ScriptUserControls (from the AJAX Control toolkit) to allow for easy script dependency injection and use a MVVM or MVP pattern to separate concerns on the server. There tends to be very little code in the ScriptUserControl code-behinds or VM/P's because essentially all they are doing is bootstrapping the client behavior script. The real work is being done by the client behavior and the REST bus. You pass settings and parameters as script properties to the client behavior, so zero inline code (and very little markup) in the .ascx. ASP.Net MVC would probably fit very well here as well - especially in the REST bus implementation. I say probably because, I haven't had an opportunity to try out MVC in my legacy app yet. But I have been able to successfully reuse this recipe and all the client script in a Java appliction based on the Spring Framework, which is nearly identical to ASP.Net MVC. And boy, talk about separation of concerns. All the view is encapsulated in the client scripts. For the most part, zero inline code and super semantic markup. The model is in the classes used by the REST bus to generate the return messages, and the controller is the code-behind for the REST bus. Almost every class has specific and targeted functionality making each class small and easy to understand and ma

      1 Reply Last reply
      0
      • C Chris Maunder

        Back in the dark ages when we wrote ASP pages, our only real choice was to inter-mingle our code and our HTML. We'd write a little HTML, then open a script block with <%, then emit some data, or start a loop, or open a connection to a database or whatever. Then we'd close the block and continue on with HTML. Then came ASP.NET and WebForms and suddenly the code-behind (and then code-beside) model dictated that there was a separation of code and markup. You were meant to be able to scatter your server-side controls onto a page like any other markup, code against it in the code-behind, then send just the .aspx page with the markup to a designer who would make it perfect and who would never be able to break you code, since there was no code on the page. Then came MVC and suddenly we're back to inline code. Well, not necessarily code - more inline binding of the properties of the view-model. Except where you need to loop. And maybe that bit where you need to branch. And maybe that other bit, too. The whole argument for the clean, precise separation of code and markup seems to never be mentioned these days and so I'd like to know how ASP.NET developers feel about this. Regardless of what framework you use, do you mix it up like it's 1999? Do you stick to your guns and keep Church and State separated? Or do you embrace the inline binding of view-model to view and just not get carried away too much. If possible?

        cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

        P Offline
        P Offline
        Paul Gehrman
        wrote on last edited by
        #27

        I definitely prefer keeping all code out of the markup, even though I coded in asp for years and am very comfortable with that approach. To me, this is one the HUGE turnoffs (among others) of MVC, going back to this messy model.

        1 Reply Last reply
        0
        • C Chris Maunder

          Back in the dark ages when we wrote ASP pages, our only real choice was to inter-mingle our code and our HTML. We'd write a little HTML, then open a script block with <%, then emit some data, or start a loop, or open a connection to a database or whatever. Then we'd close the block and continue on with HTML. Then came ASP.NET and WebForms and suddenly the code-behind (and then code-beside) model dictated that there was a separation of code and markup. You were meant to be able to scatter your server-side controls onto a page like any other markup, code against it in the code-behind, then send just the .aspx page with the markup to a designer who would make it perfect and who would never be able to break you code, since there was no code on the page. Then came MVC and suddenly we're back to inline code. Well, not necessarily code - more inline binding of the properties of the view-model. Except where you need to loop. And maybe that bit where you need to branch. And maybe that other bit, too. The whole argument for the clean, precise separation of code and markup seems to never be mentioned these days and so I'd like to know how ASP.NET developers feel about this. Regardless of what framework you use, do you mix it up like it's 1999? Do you stick to your guns and keep Church and State separated? Or do you embrace the inline binding of view-model to view and just not get carried away too much. If possible?

          cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

          B Offline
          B Offline
          BobRoeder
          wrote on last edited by
          #28

          I maybe the only one that looks as MVC and inline code as a blessing. Yes by far it is harder to maintain, but the possibilites it can open up far exceeds the ability maintain it. This is probably because I have been programming for nearly 40 years, I have different point of view on coding. I have programmed in Assembly, qbasic, fortran, pascal,c, c++, lisp,sql, html,xml,c#..... I have found .Net in gerneral, until MVC, to be quite limiting. It is more interesting now with MVC. RC

          1 Reply Last reply
          0
          • C Chris Maunder

            We've had the job posted for ages but no many responses. Ah well, we're short on glasses anyway.

            cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

            F Offline
            F Offline
            Fabio Franco
            wrote on last edited by
            #29

            Chris Maunder wrote:

            Ah well, we're short on glasses anyway

            I'll bring a whole set of imported (to you) glasses and the awesome cachaça[^]. Mind handling immigration paperwork? Where do I apply?

            1 Reply Last reply
            0
            • C Chris Maunder

              Back in the dark ages when we wrote ASP pages, our only real choice was to inter-mingle our code and our HTML. We'd write a little HTML, then open a script block with <%, then emit some data, or start a loop, or open a connection to a database or whatever. Then we'd close the block and continue on with HTML. Then came ASP.NET and WebForms and suddenly the code-behind (and then code-beside) model dictated that there was a separation of code and markup. You were meant to be able to scatter your server-side controls onto a page like any other markup, code against it in the code-behind, then send just the .aspx page with the markup to a designer who would make it perfect and who would never be able to break you code, since there was no code on the page. Then came MVC and suddenly we're back to inline code. Well, not necessarily code - more inline binding of the properties of the view-model. Except where you need to loop. And maybe that bit where you need to branch. And maybe that other bit, too. The whole argument for the clean, precise separation of code and markup seems to never be mentioned these days and so I'd like to know how ASP.NET developers feel about this. Regardless of what framework you use, do you mix it up like it's 1999? Do you stick to your guns and keep Church and State separated? Or do you embrace the inline binding of view-model to view and just not get carried away too much. If possible?

              cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

              T Offline
              T Offline
              TheyCallMeMrJames
              wrote on last edited by
              #30

              Not too long ago I got into a debate with some folks from Thoughtworks over lunch on a Monday. They were attacking dynamic data templates and the whole ASP.NET paradigm. I presented a scenario of a simple, temporary internal application for a small department in a large organization and asked how they would code it. They got into design patterns and unit testing and concern separation and automated UI testing (and on). They guessed it would take a relatively small team about a week to pull it off. Using the templates approach, I had completed the entire thing in 12 hours over the weekend. Which was the correct approach? I think that, like many programming questions (should I unit test this?), separation comes down to size and scope of the project. MVC 3 (especially using Razor) allows us to make the binding bits very clean. You can farm most ifs off to partials and keep your primary views fairly clean that way (binding and loops will remain). I think that over-separation of concern can be as much a problem as muddling together a big fatty-fat-fat page. When you're on a smaller project with limited scope, why not use the abilities of the view engine? Also, why do people seem to be offended by "code in the view"? I actually think that there is code that belongs in the view: why can't I if two divs meant for different flows? Why is that bad? What if UI cues from inline code is a simpler way to create a better, more usable UI than getting caught up in over-engineering and academia debate? Now...I will say that I am also a student of design patterns and MVC itself is an implementation of a classic. On larger projects I tend to give myself a level of protection from both a maintenance and an accountability standpoint; typically that means good test coverage and good practices in dev. But it doesn't mean that the odd bit of inline won't appear...

              My Latest: Google Maps in ASP.NET MVC 3 with Razor Tech blog: They Call me Mister James

              1 Reply Last reply
              0
              • C Chris Maunder

                Back in the dark ages when we wrote ASP pages, our only real choice was to inter-mingle our code and our HTML. We'd write a little HTML, then open a script block with <%, then emit some data, or start a loop, or open a connection to a database or whatever. Then we'd close the block and continue on with HTML. Then came ASP.NET and WebForms and suddenly the code-behind (and then code-beside) model dictated that there was a separation of code and markup. You were meant to be able to scatter your server-side controls onto a page like any other markup, code against it in the code-behind, then send just the .aspx page with the markup to a designer who would make it perfect and who would never be able to break you code, since there was no code on the page. Then came MVC and suddenly we're back to inline code. Well, not necessarily code - more inline binding of the properties of the view-model. Except where you need to loop. And maybe that bit where you need to branch. And maybe that other bit, too. The whole argument for the clean, precise separation of code and markup seems to never be mentioned these days and so I'd like to know how ASP.NET developers feel about this. Regardless of what framework you use, do you mix it up like it's 1999? Do you stick to your guns and keep Church and State separated? Or do you embrace the inline binding of view-model to view and just not get carried away too much. If possible?

                cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                E Offline
                E Offline
                EbenRoux
                wrote on last edited by
                #31

                Nowadays I am very cautious of one-size-fits-all solutions. With classic ASP that is what we seemed to get. There was no real separation --- I guess putting in some thought code have lead to better separation (web-classes in VB6 had some of this). So the solutions here seemed to be put everthing on the page. With ASP.NET we got better separation even though it was still possible to place code in the page. But the solution here seemed to be to separate everything. The thing with MVC is that the code I put on the page is view-specific code. It has everything to do with rendering the output. However, when performing operations or retrieving data, etc. these are encapsulated in the relevant classes, and data required by the view is passed in the model. So I don't think it is really going back to where we were but rather a matter of: put code you need on the page and code that shouldn't be on the page in classes. But again, one could've ended up with this solution using the previous options.

                1 Reply Last reply
                0
                • C Chris Maunder

                  Back in the dark ages when we wrote ASP pages, our only real choice was to inter-mingle our code and our HTML. We'd write a little HTML, then open a script block with <%, then emit some data, or start a loop, or open a connection to a database or whatever. Then we'd close the block and continue on with HTML. Then came ASP.NET and WebForms and suddenly the code-behind (and then code-beside) model dictated that there was a separation of code and markup. You were meant to be able to scatter your server-side controls onto a page like any other markup, code against it in the code-behind, then send just the .aspx page with the markup to a designer who would make it perfect and who would never be able to break you code, since there was no code on the page. Then came MVC and suddenly we're back to inline code. Well, not necessarily code - more inline binding of the properties of the view-model. Except where you need to loop. And maybe that bit where you need to branch. And maybe that other bit, too. The whole argument for the clean, precise separation of code and markup seems to never be mentioned these days and so I'd like to know how ASP.NET developers feel about this. Regardless of what framework you use, do you mix it up like it's 1999? Do you stick to your guns and keep Church and State separated? Or do you embrace the inline binding of view-model to view and just not get carried away too much. If possible?

                  cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                  M Offline
                  M Offline
                  mathomp3
                  wrote on last edited by
                  #32

                  Eh depends on the scale and scope of the project. If its a large project and has a bunch of complexities to it. Then I split the code, if its a table view of some data, or a basic IO database project then I'll "cheat" with MVC and go that route. Really just depends.

                  1 Reply Last reply
                  0
                  • C Chris Maunder

                    Back in the dark ages when we wrote ASP pages, our only real choice was to inter-mingle our code and our HTML. We'd write a little HTML, then open a script block with <%, then emit some data, or start a loop, or open a connection to a database or whatever. Then we'd close the block and continue on with HTML. Then came ASP.NET and WebForms and suddenly the code-behind (and then code-beside) model dictated that there was a separation of code and markup. You were meant to be able to scatter your server-side controls onto a page like any other markup, code against it in the code-behind, then send just the .aspx page with the markup to a designer who would make it perfect and who would never be able to break you code, since there was no code on the page. Then came MVC and suddenly we're back to inline code. Well, not necessarily code - more inline binding of the properties of the view-model. Except where you need to loop. And maybe that bit where you need to branch. And maybe that other bit, too. The whole argument for the clean, precise separation of code and markup seems to never be mentioned these days and so I'd like to know how ASP.NET developers feel about this. Regardless of what framework you use, do you mix it up like it's 1999? Do you stick to your guns and keep Church and State separated? Or do you embrace the inline binding of view-model to view and just not get carried away too much. If possible?

                    cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                    T Offline
                    T Offline
                    TNCaver
                    wrote on last edited by
                    #33

                    This recent trend away from separation of code from display has disturbed me, too, especially as it is coming from the very folks who preached separation during the last half of the 90s into the current decade. It really started to bother me when MS and all the "expert" bloggers instructed us to put the SQL query into the markup in the form of DataSource controls. Did they decide that all the maintenance horrors they said would happen when you put code in your form/markup were just a myth, or that the new technologies magically made those points moot?

                    1 Reply Last reply
                    0
                    • C Chris Maunder

                      Back in the dark ages when we wrote ASP pages, our only real choice was to inter-mingle our code and our HTML. We'd write a little HTML, then open a script block with <%, then emit some data, or start a loop, or open a connection to a database or whatever. Then we'd close the block and continue on with HTML. Then came ASP.NET and WebForms and suddenly the code-behind (and then code-beside) model dictated that there was a separation of code and markup. You were meant to be able to scatter your server-side controls onto a page like any other markup, code against it in the code-behind, then send just the .aspx page with the markup to a designer who would make it perfect and who would never be able to break you code, since there was no code on the page. Then came MVC and suddenly we're back to inline code. Well, not necessarily code - more inline binding of the properties of the view-model. Except where you need to loop. And maybe that bit where you need to branch. And maybe that other bit, too. The whole argument for the clean, precise separation of code and markup seems to never be mentioned these days and so I'd like to know how ASP.NET developers feel about this. Regardless of what framework you use, do you mix it up like it's 1999? Do you stick to your guns and keep Church and State separated? Or do you embrace the inline binding of view-model to view and just not get carried away too much. If possible?

                      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #34

                      I generally avoid inline code like the plague, but the new MVC Razor syntax seems to alleviate many of the annoyances of inline code. Any thoughts?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups