Hanalei, Hawaii 9/2/2010
438 Posts and Counting

More Crazy Talk: Inline Scripting and Slinging Text

Tuesday, October 16, 2007 -

As I've been reading through the comments to my last post it occurred to me that .NET has been around for about 7 years now, and for a lot of developers, that's all they know. The WebForm model has been accepted and adopted as "just the way you do it" and is unique to Microsoft. Sometimes it makes sense, sometimes not.

Josh Stodola left a good comment on my last post, and in part he asked:

In your example, what would you do about an edit or delete button?

I stumbled a bit when coming up with a response... I just wasn't sure what he meant! In my mind I was thinking "is he talking about a link button? A submit button? An embedded button in the grid?" and I had a really weird sense of Vertigo (I hear they're hiring btw)...

This isn't a negative response to Josh - just a musing on things he made me think about when reading his comment.

It's About Text
In his talk at MIX 07, Anders Hejlsberg had this to to say about web programming:

"It's only text... right? I mean sometimes we forget that..."

I think that's a really valuable thought: We're Text-Slingers, herding HTML using the HTTP protocol, over the TCP/IP trails...

HTML is pretty simple stuff, and it was abstracted into the WebForm paradigm so that Microsoft could wrap the "Visual Programming" approach together into Visual Studio (drag and drop on a control surface).  The concept is powerful, as we've seen. ASP.NET is good stuff all around and really lends itself to rapid programming.

To me, the most powerful feature of .NET is the backend stuff - using full-featured C#, managed code, ADO.NET, etc. It's not the designer.

Indeed one of the "issues" with WebForms is that this model (Visual development with Code-behind) sometimes just doesn't fit for what we're doing. Consider the CSS limitations of the GridView, the inability to do rollups, the generated "style=width:320%" when you want the columns to flow, etc. Without getting negative, let's just leave it at the statement that the designer has had it's issues :).

Some commenters on my last post mentioned that "I used HATE inline scripting until I tried Rails". I had that same experience as well. I invite you to think about this a bit more (if you're in the "scripting blows" camp)...

Forms and the Kitchen Sink
I want to answer Josh RE "what would you do about an edit or delete button" with as thoughtful an answer as I can (it was too big for a comment. Using the new MVC framework (or currently using Rails), I can embed a form (if I had to use POST- note that I don't need to do this if I use GET) :

    <table>
        <tr>
            <td>Option</td>
            <td>ID</td>
            <td>Product</td>
        </tr>
    <%
        //same method used for the ODS
        Northwind.ProductCollection products = new Northwind.ProductController().FetchAll();
        foreach (Northwind.Product p in products) {
    %>
        <tr>
            <td>
                <a href="MyEditPage.aspx?id=<%=p.ProductID.ToString()%>">Select</a>
                <form action="MyActionPage" method="POST">
                    <input type="hidden" name="productID" value="<%=p.ProductID.ToString()%>" />
                    <input type="submit" value="Delete" />
                    <input type="submit" value="Edit" />
                </form>
            </td>
            <td><%=p.ProductName%></td>
            <td><%=p.ProductID.ToString()%></td>
        </tr>    
    <%} %>
    </table>

This type of thing isn't possible using ASP.NET since we have a single form per page (the Kitchen Sink Approach)- embedded forms don't work. This isn't the way HTML was intended to be used (no judgement here - just pointing this out).

Some might think this is "Ugly" - I can understand that. But consider that it's not really ugly - it's just not what you're used to. To others (non-ASP programmers), the ViewState is the definition of ugly.

I don't want to get into a ViewState debate - I just want to point out that more control over your HTML can make your presentation much more powerful and enjoyable to create.

Related


Gravatar
josh - Tuesday, October 16, 2007 - For those who 'grew up' with .Net and/or find Rob's MVC form example awkward, you can learn more from Ruby on Rails or Castle's MonoRails. Both are MVC frameworks, and the syntax for editable grid would be much the same. Playing with those (both available right now, instead of waiting for MS MVC) will build a lot of confidence and understanding in MVC. Oh, and SubSonic also has MVC(ish) templates. Rob, are those going to be included in subsonic releases?
Gravatar
Zack Owens - Tuesday, October 16, 2007 -
I just want to point out that more control over your HTML can make your presentation much more powerful and enjoyable to create.


Yeah... I agree with you there.. but there are better ways of controlling your HTML than putting code in your UI... like building your own control (class inheriting from WebControl... that's my favorite). I really hate using the GridViews and ObjectDataSources... and I'm not a fan of many of the other ASP.NET "Standard Controls". Mostly because you don't know what's going down under the covers and when we CAN actually look, we can't touch :)

But what this comes down to is opinion. My opinion is that I would rather build my own control and output my HTML than use the inline scripting method and essentially have "psuedocode". You prefer inline scripting. Your choice... and it's the choice of the other developers in the world as well.
Gravatar
Rob Conery - Tuesday, October 16, 2007 - @Zack - I wouldn't think of you as Pro Choice! :):):):) I'm not sure what you're getting by putting your stuff in a control - but if it's a repeatable deal then I agree wholeheartedly (since DRY is the deal here). Control, helper class - same thing (well, I meant them to be). Good comment - thanks.
Gravatar
Zack Owens - Tuesday, October 16, 2007 - @Rob

HAHA! That was actually pretty funny :)

here's what I mean by control:

using System.Web;
using Nothwind;

namespace MyStuff.Controls
{
public class SmapleControlForRob : WebControl
{
public override void CreateControls()
{
ProductCollection col = new ProductCollection.Load();
foreach(Product p in col)
this.Controls.Add(new LiteralControl(p.Name));
}

}
}

Then I reference that in my page. Usually I have properties and ellaborate things... but that's basically how I shove logic into my UI... the more "Code in the DLL" the better for me!
Gravatar
Shawn Oster - Wednesday, October 17, 2007 - You bring up a good point and something I forget a lot, not everyone has been brought up through the trenches of classic server-side scripting. When I first broadened my horizons into web application development we had a choice, either ASP or this new fangled thing coming down the pipeline called ASP.NET. I had the decision power and went with ASP and even though it meant we were slightly behind the curve when we transitioned to .NET 1.1 I'm glad we started with ASP as it made learning PHP much easier and in turn made me appreciate both ASP.NET and Rails more.

Inline code is like everything else in programming, it can be done horribly wrong or it can make your life much easier and it's experience or raw aptitude that usually determines which. I've seen some PHP frameworks that use inline scripting and it's some of the cleanest code I've ever used while on the other hand I've also seen some painful abuses of ASP.NET.

In fact... in ASP.NET I've seen "Spaghetti Controls", layer upon layer of one-off, non-DRY user and server controls all done in the name of encapsulation, with crazy properties being exposed like old men's eyebrows going off in all directions in order to link everything together properly.
Gravatar
Torkel - Wednesday, October 17, 2007 - I used to really like doing custom web controls as well. The problem is though that you very easily fall into doing business logic in those controls, and there by tie you presentation logic to your business logic. Just the fact that post backs goes directly to the control is very bad from separation of concerns perspective. Since staring using MonoRail my view of WebForms has been totally altered. I used to think WebForms was ok, but after working with MonoRail and learning how powerful the MVC model is for web development it is now really painful to work with WebForms because most scenarios are so much simpler and easier to do in MonoRail.
Gravatar
Joe Brinkman - Wednesday, October 17, 2007 - Anytime I see someone in a discussion say "you very easily fall into..." I disregard the rest of their argument. I could just as easily fall into doing business logic in my inline code... I could just as easily fall into doing business logic in my code behind... But, if I am a disciplined programmer I could just as easily continue to be disciplined and create great code, whether I am doing it inline, in code-behind or in a webcontrol. I don't care what programming style you choose, if you are not disciplined in your use of that style, you will fall into bad habits. To me this whole discussion creates a false dichotomy - an all or nothing mentality, which I know is not what Rob intended. There is nothing wrong with a little inline code. There is nothing wrong with keeping a strong separation of presentation and code. There is no right answer. The design pattern du jour will soon fall out of favor. Everyone touting the benefits of MVC will soon enough move on to the next big pattern, and MVC will fall by the wayside. What matters at the end of the day is not whether we are using one pattern or another, or whether our code is in one form or another, but rather, does our code get the job done (ie meet the requirements) and can our code be reasonably maintained. If you can answer yes to both of those questions, then the rest is just window dressing.
Gravatar
Josh Stodola - Wednesday, October 17, 2007 - Hehe, didn't expect you to blog another post because of my comment :D

By the way, you really need to fix your comments system to properly HTML encode the message. It just ruined my comment on the prevous post, and as a guest of your site that is rather irritating.

I think Joe is right, this more of a personal preference; there is no right answer. But I must say that I would hate to be forced to follow this approach. To me, it would be the equivalent of being forced to dial up to the internet, and run Windows 98. It's seems a decade old! And, PLEASE, compare your method's number of lines of code needed with that of the ASP.NET web form model (including the edit/delete functions).

The thing that I dislike about this approach is that you have to have a separate page to perform the edit and delete functions. It's just not practical to me (with regards to this day in age), and it seems that problems are inevitable. For example, say the page that contains your gridview is controlled by a query string. The grid views data is reliant on the query string. Now they click your edit button and go to the action page. They perform the edits, and click the Save button accordingly. Once you update the record, how will you get them back to the page containing the GridView of data they were just seeing?

In your comments and such you seem to be hinting that I just must be just another ignorant soul that knows nothing but .NET. Well, you are wrong. I never touched ASP.NET until I was thoroughly taught JSP and Servlets. I know a little about ColdFusion as well. I've been down the inline path, trust me. Based on my experience (yeah, it is limited compared to yours, but I do have some), ASP.NET is the best overall. While JSPs and Servlets might be a little better performing in certain cases, I'm willing to take that hit becuase it's totally worth it. In college, we were assigned a JSP/Servlet project doing the basic web application (similiar to what you are demonstrating here). After college I started learning ASP.NET and I did the same project in 1/3 of the time, easy. Not to mention the difference in the number of lines of code. I couldn't believe it.

And for the record, I don't use the designer in Visual Studio. Never. I design with a plain HTML file and CSS. When it looks good enough, then I bring it in to VS and add some server-side magic. I hate the designer.

Best regards...
Gravatar
html » More Crazy Talk: Inline Scripting and Slinging Text - Wednesday, October 17, 2007 - [...] the rest of this great post here [...]
Gravatar
Rob Conery - Wednesday, October 17, 2007 - @Josh: "In your comments and such you seem to be hinting that I just must be just another ignorant soul that knows nothing but .NET" NEVER. The thought just occurred to me that not only have we "done it this way" for the last 7 years, but it's all that a lot of people know. No offense meant, and apologies if you took it that way. In terms of "It seems a decade old" - it's not. As I point out, the rest of the world does it this way - MS is unique the the "old" perspective comes from WebForm deviance. No, you don't have to use separate pages - you can use the same page if you want. Some folks like to use "Action pages" and all they do it work the DB - this is sort of like Controllers if you will. Again Josh - appreciate the comment you left; it got me to thinking a lot and spawned this post. I really hope you don't think I was picking on you - I surely wasn't!
Gravatar
Josh Stodola - Wednesday, October 17, 2007 - Hey Rob, I did not think you were picking on me at all! In fact, I got a sense of honor this morning when I seen that I was able to stir up some thoughts. So, thank YOU for actually thinking about my comment and taking the time to respond appropriately, the majority of others would not have done that.

Much respect, my friend :)
Gravatar
John Sequeira - Friday, October 19, 2007 - Rob,

Did you not get enough grief over stored procedures vs. inline SQL? You had to kick the inline scripting hornet's nest?

It's the same metaargument -- some conventions work at one level of complexity and don't at another.

Some people/teams need rules like 'no inline scripting' or 'no inline SQL' and of course it's easy to get bitten by either so you can justify it however you like. Fundamentally these rules exist more so team members don't have to make ad-hoc determinations of when they're appropriate and less because of rational cost-benefit re:when the approaches are actually fine.

Keep kicking those nests - it's always entertaining,
Gravatar
Rob Conery - Friday, October 19, 2007 - @John LOL that's funny. I seem to be a "nest kicker" - these two posts are nuttin compared to my Rails post :). WebForms has pushed us soooo deep into a single way of thinking about Web programming that it's almost disturbing when you try to do it like the rest of the world :). If I can make people think, just for a second, about an alternative then I'm happy :). You're right - it might not always fit but a good programmer will at least evaluate their options (and moreover be ABLE to evaluate the options).
Gravatar
Jeremy D. Miller -- The Shade Tree Developer - Saturday, October 20, 2007 - It's Smart to Challenge the ASP.NET Status Quo... One of my personal goals for our little ALT.NET movement is simply challenging the status quo for building...
Gravatar
Joe Chung - Sunday, October 21, 2007 - It's trivial to do what you did with inline coding, but try adding sorting, in-place editing, paging, templated columns, etc., and your inline script version becomes the "spaghetti code" ASP programmers, especially the ones doing maintenance, have come to hate.
Gravatar
Rob Conery - Sunday, October 21, 2007 - @Joe: again - for you, probably. Sorting is trivial. In terms of "in place editing" - I'll give you that. I don't like using in-place editing, but that's just me. If I needed to, I'd use a GridView. Templated columns are a WebForm thing - no such deal with scripted pages. I'm one of those guys that "does maintenance" amigo. I've unwound some seriously WHACK gridview/eventing spaghetti in my day, and simplified it to one page with inline script. Easy to read, easy to maintain without the code spread out over two pages, lost in event wireups, etc. You do realize that the rest of the dev world, outside of MS (which is the heavy majority), do it this way? Don't you think you're being a tad elitist?