Ray asked a great question a couple weeks ago that I meant to reply to but just never got around to it. He asked why ASP.NET doesn’t include an <asp:query> tag to enable developers to quickly embed SQL queries into an ASP.NET page. The responses seemed to agree with Ray’s assessment that the lack of an <asp:query> tag was shortcoming of ASP.NET, but I think it’s something more than that. If you look closely at the tags (oops! ‘controls’) that Microsoft chose to include in the System.Web.UI.WebControls namespace, you’ll notice that *none* of them give you the ability to declare variables, read a file, query a database, or send email. In fact, all of the controls are about outputting either form components (buttons, drop down lists, checkboxes, etc..) or regular HTML (like images, table cells, table rows, etc.) Microsoft didn’t leave it out because they didn’t have time or money to do it, they left it out because they don’t think you should be writing web applications using tags/controls. Using a code-behind class and controls leaves you with a much cleaner page, one that contains very little, if any scripting code and one where the design is truly separated from the logic. Not one to shy away from an example, compare these two blocks of code that create a drop down list in a form from a query. First in ColdFusion:
<cfquery name="users" datasource="#mydatasource#">
select id,username FROM users
and then in ASP.NET:
// in code behind class, MyDBWrapper.Query returns a DataSet object
DataSet users = MyDBWrapper.Query("select id, username FROM users");
userid.DataSource = users;
<!--- asp.net page --->
<asp:dropdownlist id="userid" runat="server">
That’s a pretty lame example, but the main takeaway from it is that all of the ASP.NET controls can be manipulated from the code behind class, which is something you cannot do in ColdFusion. You can declare a drop down list in your code behind, bind it to a datasource, modify the selected value, toggle it’s visibility, or modify any of the other 20 or so properties that exist for a drop down list. To me this provides true separation of presentation from logic. Page designers can drop in ASP.NET controls and I can sit in a dark room and modify anything and everything on those controls from my codebehind. You can’t do that in ColdFusion.
Does this mean that ASP.NET is better than ColdFusion? Nope. Notice that I have to create a code behind (technically I could write my code in a <script runat=”server”> block) and that I have to create my own database wrapper class. ASP.NET takes a little bit more work. ColdFusion is much faster to develop in but arguably encourages you to intermingle code with display. ASP.NET takes a bit longer, but developed correctly, encourages you to separate your logic and display code.