Complex DataBinding with Date Formats in .NET 1.1

Did you know that you can do some pretty complex things with .NET's late binding in ASP.NET 1.X? I had a problem where I needed to be able to kick out dates in a specific format. I didn't want to use a helper method, which involves compiled code, so I found a solution.

1<%# CType(DataBinder.Eval(Container.DataItem, "DateAdded"), DateTime).ToString("dd MMM yyyy") %>

Your datatype on the DataItem can be a string or a DateTime object. Either way, you must do the conversion first, otherwise there would be an implicit conversion, which is not allowed in late-binding code. The bold part is the name of the date property on the object you are binding against.

As with any late-binding, you'll have a slight performance delay on the first hit, but that's just cause the page is recompiling. It'll be fine after that.

4 Comments

Comments have been disabled for this content.