Can I use Extension Methods inline in an ASPX page? - asp.net

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.

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.

Import HTML page in .NET

The following code imports an HTML page (which simply holds one table) into my .ASP page. This works great, but I now am converting into .NET and am having obstacles.
<% Response.Write(getFilesContent("table.htm")) %>
This code does not work in .NET, and I read that this method is not recommended or widely used? Are there any thoughts, advice, or solutions about this?
I simply want to import this HTML page to read in a content box within my .NET page. In essence, the .NET page is hosting the HTML table.
Any help would be greatly appreciated.
Thank you in advance for your time and help.
To get you started, take a look at the System.Net.WebClient class http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.80%29.aspx
In particular, the "DownloadXXX" methods.
You could put the table in an asp.net user control
A asp.net custom user control can act like an include that encapsulates asp.net
markup.
here is a tutorial:
http://ondotnet.com/pub/a/dotnet/excerpt/progaspdotnet_14/index1.html
After you create the control add a reference
<%#Register tagprefix="uc" Tagname="html" src="custom_html.ascx" %>
then just a the control markup (in this case <uc:html runat="server"/>)
It would be cool the create a control the reads an html file by adding a src property
Try this:
<% Response.Write(New StreamReader(Server.MapPath("~/table.htm")).ReadToEnd()) %>

What functions are allowed inside <%# ... %> tags?

In ASP.NET, what functions are allowed inside the <%# %> tags? I frequently use Databinder.Eval(), and I know some basic things like CStr(), but where can I find a complete list with documentation? I would look myself, but honestly I don't even know what the name of the <%# %> tags are.
It's funny that nobody really knows what these are called - I think in the ASP.NET MVC team they call them Code Nuggets. Others call them Code Rendering Blocks.
Anyway, this is essential reading: http://quickstarts.asp.net/QuickstartV20/aspnet/doc/pages/syntax.aspx
Here is some specific info on the <%# Data Binding Syntax: http://msdn.microsoft.com/en-us/library/bda9bbfx%28v=VS.100%29.aspx
And this helped me understand the Eval voodoo: http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx
Anything that is in scope. E.g. public/protected methods on your page, public methods in some referenced namespace/class, etc. In addition to things that are related to the current NamingContainer you're within.

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?

Register User Control Issue

I have a user control registered at the top of my page:
<%# Register Src="/Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
and I reference it in my page like this:
<pmc:Navbar runat="server" id="navbar"></pmc:Navbar>
but it does not know what <pmc:Navbar is. I cannot figure out why.
I'm using VS 2008, in a Web Application Project.
Maybe you should specify the path with ~: ... Src="~/Controls/User/Navbar.ascx" ...
Remove either the initial slash from the path to the control, or better still, prefix it with "~" :
<%# Register Src="Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
or
<%# Register Src="~/Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
The first solution is flakey as it relies on the page existing in the root folder and the control existing below it. The second is the preferred as it will work from any page in your project.
You should also consider registering your user controls in your web.config, as it keeps things much neater, and tends to avoid path issues a little better.

Resources