views:

114

answers:

2

With a .NET repeater is there anyway to call methods in the #Eval('') directive? For example, if I am binding to an object with a DateTime property, it would be really convenient to set #Eval("ADateTimeProperty.ToString('hh:mm')") or something along those lines. Is the only option to create another property with that returns a formatted DateTime?

+4  A: 

Absolutely. For your datetime issue, however, you can just use:

<%# ((DateTime)Eval("ADateTimeProperty").ToString("hh:mm") %>

If you wanted to call a method, you could do:

<%# MyCustomMehtod(Eval("ADateTimeProperty")) %>

And on the code-behind:

protected string MyCustomMethod(object input)
{
    return DateTime.Parse(input.ToString()).ToString("hh:mm");
}
Chris Pebble
Thanks Chris, much appreciated!
Daniel
+2  A: 

You can use the overload of the Eval method that takes a format string as a second parameter:

<%# Eval("ADateTimeProperty", "{0:hh:mm}") %>
Julien Poulin