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

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.

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.

In ASP.NET what is the name for HTML directive symbols <%# or <%= etc for executing code on the server side?

Was trying to reference this the other day, and I've heard them called several things.
They are intrinsically hard to google for. Does this syntax have a proper name? Thanks!
The Visual Web Developer Team calls them "Code Nuggets," but I don't think there's an official term that'll help you find them in MSDN.
You might be interested in the following MSDN articles:
Code Render Blocks
Data-Binding Expression Syntax
Web Forms Syntax Reference
I call all variations of <% %> server tags. Dont know if they have another name.
I've always known them as "Expressions" (and/or "Code Expressions", "ASP.NET Expressions", and a few other variations).
I've heard that they are called constructs.
I think they don't have a collective name. The links to MSDN in this article all call them with different names - Embedded Code Blocks (pure <% %>), construct (<%= %>), Data-binding syntax (<%# %>), ASP.NET Expressions (<%$ %>, Directives (<%# ... %>), Server-side comments ( <%-- ... --%> ). The article itself calls them inline ASP.NET tags.
AFAIK, there is no common name for these constructions, but we use inline asp.net tags (* found a while ago here).
MSDN refers to them as Embedded Code Blocks.

Information on <%# ... %> in .NET

Can someone point me in the right direction of some information on using:
<%# ... %>
It seems Google does not like <%# as a search term. I don't even know what such a construct is called.
Or maybe someone can give me a brief description of how it works?
It is a data-binding expression. Look here for a start.
This is used to reference DataItems in a DataBound control. Here's the MSDN ASP.NET data binding overview.
It's data binding syntax
http://www.15seconds.com/Issue/040630.htm
It can also be used to get around the error "The Controls collection cannot be modified because the control contains code blocks" as seen here link

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.

Simple HTML construction in ASP.NET?

A simple question, I think:
I want to put a tag into an ASP.NET app I've been asked to maintain, so I'm coming at this from a newbie point of view just tinkering around the edges without knowing a lot.
I wrote an old ASP application back in 1998, so I am just running on memory...
How do I write some output to the webpage?
I know I can use an
<asp:label id="blah">
but then I need to define a Label blah; in my code behind and then assign it.
I believe that I can put in-place:
<% Response.Write("sometext"); %>
and that will write sometext in the location within the page. (Am I correct?)
Lastly, I remember there was a syntax to the effect of
<%= "some string" %>
but I can't find the documentation on it, to say either it is deprecated, unadvised, or the rationale for such a decision.
I have tried googling for "ASP.NET grammar" but I can't even find a good description that "<%=" even exists, though it is mentioned in a few blogs.
For something simple, like inject the global version number, or the current date, then I can't see anything particularly wrong with in-place composition - it would save me defining 15 labels and having to initialise them all - though perhaps the asp:label approach could reference one global instance of a label?
Just asking for opinions on good practices :)
<%= string %> is perfectly valid ASP.NET syntax. The reason you will often find references to problems with using that is people use <%= (equivalent to Response.Write) when they should use <%# (for databinding) or vice-versa.
For example, we use it very extensively in our content managed site, where we pull in values from a global settings repository:
<%= SiteContext.Current.GetSetting("SiteTitle") %>
MSDN:
MSDN entry on <%= (this is under the JScript.NET section but still applies)
MSDN entry on <%#
Some others suggest <%= is not a "best practice" or a very good approach, but I strongly disagree with that sentiment. For an MVC-ish type site (especially a site that is template- or view-driven in some way), the most direct approach is frequently more effective than using server controls.
Just be mindful that when you use an <asp:Label /> it renders the .Text inside the <span> tag whereas an <asp:Literal /> adds no extraneous HTML to the string passed to it.
For example, if you were building a content management system and wanted to display user-driven HTML, a Label control would not correctly display the output from a WYSIWYG type rich textbox whereas a Literal control is the appropriate choice.
The <%= %> is the late-bound equivalent of the Literal's .Text property. The only difference here is when the value is placed in the page (aside from obvious syntax and separation of concerns paradigm) during the course of the page lifecycle.
Since the .Text property is on a control inherited from WebControl, it can be set/read/manipulated during any of the events following the control's Load event (wherever/whenever you load the control inside the page), but the <%= %> text cannot be directly read/used/manipulated by the code-behind without referencing some other control to get to it (like a containing div's InnerHtml property).
There are lots of options. You could use a single label, and string concatenate all the data you want displayed in that location.
You could create a user control with the layout you want and assign values that way.
You could inject it directly with response.write or the <%= %> syntax
You could create an HtmlGenericControl in your code behind (it's a div), add some text to it, and inject it into the pages controls collection.
Whatever you pick, try and go with the existing style of the coded page.
Look up the term "render blocks" for the <% %> syntax.
How about using
<asp:Literal id="z" text="goofy" runat="server" />?
Labels are usually used with forms.
You can also take full control of the rendering of your pages and controls and compose whatever you need to. You control the HTML, the order of rendering your controls, etc...
Go with the <asp:label /> (or a literal control if you want to customize some html in the content). Seriously. I'ts not that hard: when you put label in your markup visual studio will create it in the code-behind for you, so there's no extra work involved.
You could use the <%= "some string" %> syntax in web forms, but there can be issues when mixing that with the asp controls and there's a good reason new frameworks moved away from mixing logic like that in with your markup.

Resources