ASP.NET vs anything

April 22nd, 2009

I read yet another ASP.NET vs PHP post. It got me thinking…

In case you can’t tell by looking at the category listing over on the right, my primary focus is the back-end of intranet applications using the .NET framework. I tinker with winforms and console applications, but that isn’t what I spend most of my time doing. Code behind files, libraries, windows services, and web services take up most of my daytime life.

There is a LOT to know about .NET. There are a multitude of certifications, and the track for web applications is different from windows forms applications. It takes years to master. I am constantly learning, even after over 3 years coding with it.

Notice how I said “the .NET framework”? I didn’t say C#.NET or VB.NET. The .NET framework is exactly that — a framework. C# is a language. C++ is the bastard stepchild no one talks about but many use and love. You can see where this is going, can’t you?

You cannot compare .NET to PHP or Java and have it mean anything to me. Comparing a framework to a language is apples and oranges. If you want to compare language features, even VB, C++, and C# have differences within the .NET framework.

If you want to post about which development framework someone should choose for their web application, I want to see you pick another actual framework to compare it TO. There are a multitude of frameworks available for PHP. There are over 100 for Java, according to this pdf sheet from Sun called “Choosing A Java Web Framework“. I haven’t used any of them. I want to know what the framework can do, not what the core language supports.

I love the .NET Framework. I’ve played with PHP enough to be dangerous. I can mess with WordPress and make quick scripts for servers that only have PHP on them. I know enough Java to be dangerous, too, and coded with it for 5 years, but it was long enough ago that most everything has changed (I started back on java 1.2, before they even had regular expressions and I had to hack Netscape Enterprise Server on a Solaris box to get JSP working, because they wouldn’t let me use Apache).

I do know, though, that there are frameworks for those languages, and they have different capabilities, strengths, and weaknesses. If I want to compare ASP.NET to Java, I need to detail frameworks like EJB. If I want to compare it to PHP, I need to actually research the various PHP frameworks, like Zend, to see if they offer particular features.

I’m pretty sure that PHP and Java frameworks are complex enough that someone who normally develops with .NET would not be able to tell what they can and cannot do without looking it up in the data sheets. I am also pretty sure they take a decent amount of time to master, just like .NET does.

My point is — if you want to write up a great comparison for people to choose a web development platform, you have to compare apples to apples.

What do you think? Since PHP and Java are separate from their frameworks, but C#.NET and VB.NET are really subsets of the .NET framework in its entirety, is it fair to compare? Shouldn’t people be comparing Zend to ASP.NET, or EJB to ASP.NET, not PHP or Java to ASP.NET?

I’d love to hear what you think. Let me know in the comments!

Using XSLT For Dynamic Content

April 20th, 2009

I read this post on XSLT and dynamic content in ASP.NET, and at the time, I didn’t think it applied to anything I did. I’m always willing to experiment with new code and I do try to be open minded about implementation, though.

It struck me that the author’s comment on when to use this design never applies to me for the applications I work with daily:

“Best suited for applications with high data complexity and ones that are expected to change significantly and frequently. In addition, this allows for a lot of room for creativity as far as interface design goes.”

Also, in the example code, there are no server controls (at least not that I saw). There isn’t a single application I’ve ever done in ASP.NET that didn’t need to postback and manipulate controls, check what the user entered, and so on. So, I filed the idea for later.

I bring this up because when I was trolling the asp.net forums for an idea for a post and saw what looked like a pretty easy question on how to implement a simple trivia question/answer page, I thought I could get some practice with a few different ideas, including this one – the idea of generating content and controls with transformations.

This post is the result of my deciding to play. I won’t say it’s the best way to implement it, only that it is A way to do so. I wanted to use VB.NET (after a blurb in a post on another site mentioned how few good examples are available in the language) and XSLT transforms and not rely on a database like I normally do, and I wanted to see if I could use XSLT and server controls together, so I used this idea of a little trivia to do that.

Read the rest of this entry »

Using CustomValidator To Validate CheckBoxes

March 4th, 2009

For your viewing pleasure, a quick and dirty example of using a CustomValidator to check that there is at least one CheckBox checked, but no more than 3 CheckBoxes are checked. This example uses both client-side validation and server side validation.

The ASPX:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomValidatorCheckbox.aspx.cs" Inherits="CustomValidatorCheckbox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Custom Validator - max number of checked items</title>
    <script type="text/javascript">
    function petValidate(sender, args)
    {
        var count = 0;
        var values = new Array(
            document.getElementById('ckDog').checked, 
            document.getElementById('ckCat').checked, 
            document.getElementById('ckBird').checked, 
            document.getElementById('ckFerret').checked, 
            document.getElementById('ckOther').checked);
        for (var i = 0; i < values.length; i++) 
        {
            count += values[i] ? 1 : 0;
        }
        args.IsValid = (count > 0 &amp;&amp; count <= 3);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Custom Validator Example<br />
        <br />
        Pet(s): (select up to 3)
        <br />
        <asp:CustomValidator ID="cvPetValidator" runat="server" Display="Dynamic" ErrorMessage="You must choose at least one and no more than 3 pets."
            OnServerValidate="cvPetValidator_ServerValidate" ClientValidationFunction="petValidate"></asp:CustomValidator><br />
        <asp:CheckBox ID="ckDog" runat="server" CausesValidation="True" Text="Dog" /><br />
        <asp:CheckBox ID="ckCat" runat="server" CausesValidation="True" Text="Cat" /><br />
        <asp:CheckBox ID="ckBird" runat="server" CausesValidation="True" Text="Bird" /><br />
        <asp:CheckBox ID="ckFerret" runat="server" CausesValidation="True" Text="Ferret" /><br />
        <asp:CheckBox ID="ckOther" runat="server" CausesValidation="True" Text="Other" /><br />
        <br />
        <br />
        <asp:Button ID="brtSubmit" runat="server" Text="Submit" /></div>
    </form>
</body>
</html>

The Code Behind:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CustomValidatorCheckbox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void cvPetValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        int count = 0;
        bool[] values = new bool[] {
            ckDog.Checked, 
            ckCat.Checked, 
            ckBird.Checked, 
            ckFerret.Checked, 
            ckOther.Checked};
 
        foreach (bool b in values) count += b ? 1 : 0;

        args.IsValid = (count > 0 &amp;&amp; count <= 3);
    }
}

Note that the client-side javascript is assuming that the controls actually have those IDs. Depending on the structure of your page, they may get renamed due to NamingContainer conflicts. If that happens, you’ll want to set out the script to use the real client ids of the controls instead, like so:

document.getElementById(’<%= ckDog.ClientID %>‘).checked

I’ve seen a lot of confusion over validating checkboxes, so hopefully this helps someone. Happy coding!

GridView Full Example

March 3rd, 2009

I decided to post a basic example of creating a GridView with in-place editing and deleting, with drop down lists for the ID fields, using SqlDataSources for them. Sorting and paging are supported. Inserting is not. No styles are applied or anything, and all paging/sorting is done the default way, on the client (that means it’s not a great way to do things for large datasets). If this were a live site, I would also configure caching for the data sources for the suppliers and categories, as they wouldn’t be changing a lot.

For this example, you will need the Northwind database and a connection string in your web config. Also, I assume you’re using Visual Studio 2005 with .NET 2.0.

Here is what the page looks like when you run it (remember, no styling, it’s ugly). Click the thumbnails for the big pictures.

GridView Ex 1

GridView Ex 1

Read the rest of this entry »

ASP.NET to officially support jQuery

October 1st, 2008

Check it out – a blog post from Scott indicates that .NET, and Visual Studio, will support jQuery!

Scott says

“I’m excited today to announce that Microsoft will be shipping jQuery with Visual Studio going forward. We will distribute the jQuery JavaScript library as-is, and will not be forking or changing the source from the main jQuery branch. The files will continue to use and ship under the existing jQuery MIT license.”

This is exciting news for developers, as jQuery will help fill the noticable gap in ASP.NET to be truly client rich. I know things I’d like to do, such as supporting dragging and dropping of elements in a grid (a la Netflix queue), are not possible with the out-of-the-box version of Studio / ASP.NET.

I’ve played with ASP.NET AJAX, but found the performance to suck, and the component set to be ugly and difficult to style. At work, we use a popular third party component set called Infragistics, and their components do perform well, but they are not free. I’m excited to hear about jQuery support because I think it will really open up the playing field for those who can’t afford third-party component sets.

Do you use jQuery? What do you think of it? Does it perform well? Do you think it will take off for the .NET world?