Formatting a phone number in ASP.NET data bound controls

As part of my ongoing “stop coding like it’s 1999″ regime, I managed to think my way out of some code in the following scenario:

My business object returns a phone number, but it happens to be a raw number, i.e. there’s no formatting: (416) 967-1111 comes back as 4169671111. I’ve got a repeater control in ASP.NET, and I want to pretty it up.

In the past, I’d:

  1. Code a new property into the business object to give me a formatted string.
  2. Add an ItemDataBound event handler to the repeater, which calls FindControl and fills in the value all pretty-like.
  3. Ignore the issue and hope nobody notices.

The new and improved Luke does this in his <ItemTemplate> node:

<%# String.Format(
   ”{0:(###) ###-####},
   Convert.ToInt64(
      DataBinder.Eval(Container.DataItem, “PhoneNumber”)))
%>

Not the prettiest thing in the world, but (after some thinking) the easiest, and it actually puts the presentation details at the right layer.

If I’m not careful, I might actually become productive.

Pop quiz: is there a cleaner way to do this?