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.
posted on Thursday, September 30, 2004 2:31 AM
Feedback
# re: Complex DataBinding with Date Formats in .NET 1.1 9/29/2004
11:48 PM Matt Berther
How about
DataBinder.Eval(Container.DataItem, "DateAdded", "dd MMM yyyy")
Same thing... without the cast.
# re: Complex DataBinding with Date Formats in .NET 1.1 9/30/2004
12:02 AM Robert McLaws
Nope, doesn't work. I get "dd MMM yyyy" as my value. That's why I had
to cast...
# re: Complex DataBinding with Date Formats in .NET 1.1 9/30/2004
12:53 AM LeeB
Or you could use:
CType(Container.DataItem.DateAdded, DateTime).ToString("dd MMM yyyy")