views:

515

answers:

3

I'm trying to use some extension methods that I use to apply consistent formatting to DateTime and Int32 - which works absolutely fine in code behind, but I'm having issues with databinding.

I get:

'System.DateTime' does not contain a definition for 'ToCustomShortDate'

for

<%# ((ProductionDetails)Container.DataItem).StartDate.ToCustomShortDate() %>

(inside a templatefield of a gridview contained on a usercontrol)

Even when I'm including the namespace that the extension method is defined in at the top of the usercontrol:

<%@ import namespace="MyAssembly.Formatting" %>

Has anyone else come across this and is there any way to resolve it?

EDIT: My mistake, above should be:

<%@ import namespace="MyNamespace.Formatting" %>

ie. I'm not incorrectly referencing the namespace (works vertabim in the code behind)

A: 

You should remove the assembly name from the import namespace...

I guess your extension method is located in an namespace called Formatting.

namespace Formatting
{

    public static class DateTimeExtender
    {
        public static string ToCustomShortDate(this DateTime date)
        {
            return date.ToString("dd MMM yyyy");
        }
    }

    public class ProductionDetails
    {
        public DateTime StartDate { get; set; }    
    }
}

And in the ASPX page...

<%@ Import Namespace="Formatting" %>
Rohan West
+1  A: 

Regarding the extenstion method i implemented above should my namespace look like this?

namespace MyNamespace.Formatting
Rohan West
Yes it should - they match, it just doesnt seem to be getting picked up.
Kieran Benton
+1  A: 

Found the issue! I was including the namespace correcly as I thought - but the real issue was that the app was only INCLUDING the .NET 3.5 assemblies and not being compiled using the 3.5 compiler, was missing some entries from web.config which I realised when I created an empty project and tried it (successfully) in there!

Error messages can be so misleading sometimes...

Kieran Benton