How do I Databind Dates or DateTime in ASP.NET 1.x? VB or C# please

A

Anonieko Ramos

Answer is in an article.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")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top