Search

Posts Tagged ‘asp.net’

I’ve been working with Razor some recently, and here are a couple of surprises that bit me.

razorwire.jpg

First, I had a block of JavaScript in a view, and I was getting a parsing error on the “less than” sign in a JavaScript “if” statement. The problem was that my JavaScript block was, itself, inside of a Razor block (a ‘using’ block actually), so Razor was having trouble parsing it. I just moved the JavaScript out of the block and Razor handled it fine. See: “Tag is missing a name” with Razor on Stack Overflow for more details.

Secondly, I was writing an HtmlHelper extension method. I put the namespace in the pages/namespaces Web.config block, but my Razor view didn’t seem to be able to find it. Apparently, Razor uses a different configuration block for this, which is kinda annoying. For details, check out Razor (MVC 3 RC) HtmlHelper Extensions Not Found on Stack Overflow. According to Marcin Dobosz (who left a comment on that SO page), this is not something they are going to fix/change. However, if you are using Razor, it looks like you can remove the standard pages/namespaces block from the Web.config.

I’m not a big fan of ASP.NET WebForms, and one of my pain points is form validation.  It seems a bit heavy-handed and clumsy compared to, say, Dynamic Data or MVC.  Generally, it’s adequate.

To validate that a textbox is required is easy enough, just use a RequiredFieldValidator.  To validate the text is in some specific format seems easy enough too, just use RegularExpressionValidator.  But the “gotcha” that may not be immediately apparent is that using the RegularExpressionValidator as is requires you to violate DRY, since you must specify the ValidationExpression each time you use it.  With this, you get all the standard pitfalls of DRY, including the possibility of a phone number formatting on Form1.aspx being totally different than the phone number formatting on Form2.aspx.  So, wat do?

Here’s what I did: create a more specific implementation of RegularExpressionValidator called PhoneValidator

using System.Web.UI;
using System.Web.UI.WebControls;

namespace Whatever
{
    [ToolboxData("<{0}:PhoneValidator runat=\"server\" />")]
    public class PhoneValidator : RegularExpressionValidator
    {
        public PhoneValidator()
        {
            ValidationExpression = @"^([\(]{1}[0-9]{3}[\)]{1 ... $";
        }
    }
}

And now you can use PhoneValidator just as you would RegularExpressionValidator, except the ValidationExpression only exists in one place for consistency and ease of change.  Don’t forget to register the assembly that the above class is in on your page:

<%@ Register assembly="WhateverAssembly" namespace="Whatever" tagprefix="MyValidator" %>

 

(Or you could register it in your web.config for use in the whole app).  Once it’s registered, just do this (it should appear in intellisense as well):

<MyValidator:PhoneValidator ControlToValidate="txt_phone_number" runat="server" />

I recently had need for a ASP.NET WebForms grid that would do something you’d think would be relatively simple:

  • Allow the user to enter ‘rows’ of some set of data (for instance, multiple address/city/state)
  • Allow the user to update/delete all of these rows of data, almost like an Excel sheet
  • Keep that data in a ‘holding pattern’ on the form (via ViewState) until such time they ‘submit’ the form to be persisted, emailed, etc.
  • Not use any databinding, database backing, or session – just ViewState

I looked and I looked, but I couldn’t find anything that quite fit right.  I tried to implement a GridView using ObjectDataSource, wrapped around ViewState, but I couldn’t get it to work.  I tried some jQuery grids, GridView, etc, and these all seem to want to talk to a database (or some persistence somewhere).  I tried a plain old ListView/Repeater, but the dynamically added user controls (i.e. my ‘rows’ of data), disappear after postback.

Since I couldn’t find the perfect fit (it may still be out there), I just created my own (with some serious help from this guy).

stupidgridlogo

Yes, StupidGrid, available on CodePlex.  Why the name?

  1. It doesn’t bind to any database or persistence: once the user leaves the page, StupidGrid stupidly forgets anything the user entered.
  2. I couldn’t find a decent (free) grid to do what I wanted, so as I was looking around for one, I kept saying “I just need a stupid grid!”
  3. It’s stupidly simple to use (or, at least, that’s my goal): just build a user control you want to repeat, “feed” its path to the UserControlPath property, and you’re done.

I’ve just done an official release of version 0.1, which I’m sure has many problems, so I would be ecstatic to hear any opinions, suggestions, patches, criticisms, bug reports, problems, etc.

And if anyone wants to design a better logo or icon, leave a comment or hit me on Twitter.