How to use extensions and utility methods in markup? - asp.net

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 definition 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

Related

Why isn't ExtensionMethod recognized inside <%= %> [duplicate]

Is it possible to do something like this inline in an ASPX page?
<%= Me.SomeExtensionMethod() %>
I can't seem to figure out how to get this to work properly. I'm receiving an error saying that "SomeExtensionMethod" is not a member of the current Page object. I've added the necessary <%# Import Namespace="..." %> directive at the top of my page. This Does work in code-behind.
This isn't vitally important, but it would be good to know how to do in the future.
Thanks!
Adding imports in the namespace works for me!
<%# Import Namespace="Foo.FooFoo" %>
Try closing the .aspx page and opening it up again as per this answer. If that improves things at all (e.g. enable intellisense) but doesn't solve it, please post any new errors you get.
You could also add the Public modifier to your Module or class definition. If you're using Modules, it really doesn't make sense to me that it would be required, but some discussion on this forum indicates that it might help.
If it's working in the codebehind, add the namespace to the function call:
<%=MyNamespace.ExtensionFcn("hello, world") %>
I'd do this before I'd modify the web.config.

asp.net calling code inline not working

In my code behind, I can do the following in the onload:
string x = Fmg.Cti.UI.Utilities.Classes.ResourceController.ReadResourceValue("Riblet", "Riblet_Chicklet1_Button_text");
This works without issue.
In my aspx page (I didn't remove the code from the onload), I put this:
<%= Fmg.Cti.UI.Utilities.Classes.ResourceController.ReadResourceValue("Riblet", "Riblet_Chicklet1_Button_text")%>
When I do, I got an error:
error CS0234: The type or namespace name 'Cti' does not exist in the
namespace 'Fmg' (are you missing an assembly reference?)
I had this (or something quite similar) working. I don't know how I broke it.
Thanks again for your help.
You need to register the assemblies you're using in your page as well (just like usings in your code behind). See Do I have to add "<%# Register assembly=" to every page?
Do you have that namespace imported? For asp.net you can do something like the following to make namespaces available for inline coding.
<%# Import Namespace="Fmg.Cti.UI.Utilities.Classes" %>
You can make namespaces generally available to all of your asp.net pages for inline coding by adding the namespaces into the web.config
<system.web>
<pages>
<namespaces>
...
<add namespace="Fmg.Cti.UI.Utilities.Classes" />
...
</namespaces>
</pages>
</system.web>

ASP.NET error: The page Y.ascx cannot use the user control X.ascx

I am getting the error below when trying to build the web site project in Visual Studio 2010:
The page '/WebSite/controls/C2.ascx' cannot use the user control '/WebSite/controls/C1.ascx', because it is registered in web.config and lives in the same directory as the page.
I have 2 web user controls:
controls/C1.ascx
controls/C2.ascx
The controls have been registered in web.config:
<configuration>
<system.web>
<pages>
<controls>
<add src="~/controls/C1.ascx" tagPrefix="my" tagName="C1"/>
<add src="~/controls/C2.ascx" tagPrefix="my" tagName="C2"/>
</controls>
</pages>
</system.web>
</configuration>
C1.ascx contains just a static HTML, C2.ascx is trying to include C1:
C1.ascx contains just some plain static simple HTML. C2.ascx is trying to include C1.ascx:
<%# Control Language="VB" %>
<my:C1 runat="server" />
<p>Hello from C2</p>
When trying to build the project, I am getting the error message at the top. I realise this issue can be fixed by adding another Register directive to C2.ascx...:
<%# Register Src="~/controls/C1.ascx" TagPrefix="ctl" TagName="C1" %>
...but I'm wondering if there's a cleaner solution and why am I getting the error in the first place?
Your only possible solutions are to:
Move the control out of the directory its currently sharing with outer.ascx, or
Re-register the control inside of the outer.ascx like you already mentioned
Re-write them in code as controls in a separate library
I personally think moving is the easiest, if it will work for your solutions. Second would be re-registering, even though annoying. Abstracting them out into a full code library is probably not worth the effort if this is the only reason you are doing it.
You could also put the controls into different folders. But I don't think this is much cleaner or better.
BTW: this behavior is by design, as you can read on this MSDN page (look for the yellow note almost at the end of the page).

BeginForm is not a member of 'Html' and Encode is not a member of HTML

I'm working on this big project in MVC ASP.NET w\ VB.NET
One of my views is getting me headaches since a few and i'm not sure what's up.
I've used the Begin.Form and Html.Encode methods alot in my other views and i never had any problems. Now this new Create.aspx view for one of my object called Automation is giving me multiple build errors such as those cited in the title plus
Error 184 'Context' is not a member of
'ASP.views_automatisation_create_aspx'.
BeginForm is not a member of 'Html'
Encode is not a member of HTML
My header is as follow (just like all of my other working views headers) :
<%# Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of XXXXX_XXXXX.Automatisation)" %>
Can anyone help me out ?
Thanks everyone for reading.
Check whether you have renamed your Model.
If you are using strongly typed-view then these kind of error is there.
Note:- Even though, you have added correct namespaces in your program, and most of the errors not specifically gives you the exact problem.
Does it work if you manually import the namespace (below #page):
<%# Import Namespace="System.Web.Mvc.Html" %>
My guess is that you have deleted a <% somewhere by mistake in your .aspx file.
Did you remember to schlep the little web.config that was in your views folder around? And did you remove any of the <add /> elements in the <pages><namespaces> bits of your web.config?

Can I use Extension Methods inline in an ASPX page?

Is it possible to do something like this inline in an ASPX page?
<%= Me.SomeExtensionMethod() %>
I can't seem to figure out how to get this to work properly. I'm receiving an error saying that "SomeExtensionMethod" is not a member of the current Page object. I've added the necessary <%# Import Namespace="..." %> directive at the top of my page. This Does work in code-behind.
This isn't vitally important, but it would be good to know how to do in the future.
Thanks!
Adding imports in the namespace works for me!
<%# Import Namespace="Foo.FooFoo" %>
Try closing the .aspx page and opening it up again as per this answer. If that improves things at all (e.g. enable intellisense) but doesn't solve it, please post any new errors you get.
You could also add the Public modifier to your Module or class definition. If you're using Modules, it really doesn't make sense to me that it would be required, but some discussion on this forum indicates that it might help.
If it's working in the codebehind, add the namespace to the function call:
<%=MyNamespace.ExtensionFcn("hello, world") %>
I'd do this before I'd modify the web.config.

Resources