开发者

How to use extensions and utility methods in markup?

Ok. This is probably a really stupid question, but I'm going to ask it anyway...

How can I use extensions and utility methods in my ASP.Net markup? For example, (say) I have a DateTime extension method called "ToExampleString()" (contained in the DateTimeExtensions class in my Common.Extensions project) and I want to use it in my markup in the ListView's ItemTemplate:

<ItemTemplate>
    <span><%# ((DateTime)Eval("DateStarted")).ToExampleString() %></span>
</ItemTemplate>

I'm getting the following error:

'System.DateTime' does not contain a defin开发者_JAVA百科ition for 'ToExampleString' and no extension method 'ToExampleString' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)

The page simply can't see the extensions method.

Similarly, how do I make my page's markup aware of a utility class:

<span><%# ExampleUtility.ProcessDate(Eval("DateStarted") %></span>

What steps do I need to take to make this stuff work? I assume I'm overlooking something stupidly obvious?

Thanks


You need to import the namespace either at the top of the page as others have said

<%@ Import Namespace="Common.Extensions"%>

Or globally in your web.config

<system.web>
  <pages>
    <namespaces>
      <add namespace="Common.Extensions"/>
    </namespaces>
  </pages>
</system.web>

If you simply need access to methods of a public module (or static class), just import your application's root namespace.


<%@ Import Namespace="Common.Extensions" %>

I believe you can do that for all your markups in the web.config.


You have to import the namespace, at the top of the page:

<%@ Import Namespace="Common.Extensions"%>


Namespaces?

You should add using/import directive in aspx markup

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜