When binding values to controls I have plenty of this:
Text='<%# Bind("StartDate", "{0:dd.MM.yyyy}") %>'
However I would like to extract this format (and few others) to helper. And this is what I did:
public static class DateTimeExtension
{
public const string CalendarDateFormat = "{0:dd.MM.yyyy}";
public const string CalendarMonthFormat = "{0:MM.yyyy}";
public static string ToCalendarDate(this DateTime dateToFormat)
{
return string.Format(CalendarDateFormat, dateToFormat);
}
public static string ToCalendarMonthDate(this DateTime dateToFormat)
{
return string.Format(CalendarMonthFormat, dateToFormat);
}
}
Yet now, when I change my Bind call to something like this:
Text='<%# Bind("StartDate", DateTimeExtension.CalendarDateFormat) %>'
I get an exception:
System.Web.HttpException: A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind.
I have namespaces configured in web.config so that shouldn’t be an issue. Is there a way to achieve what I like?
Try using Eval as Bind expression is a very specialized part of ASP.NET code generation and it has a limitation that the format parameter must be a literal.
Read more about Bind and Eval here.