This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ASP.NET “special” tags
I am trying to understand an MVC application. It has multiple user controls. In these controls I see syntax like:
<%= ...some text ... %>
I have also seen:
<%: ...some text ... %>
<# ...some text ... %>
<% ...some text ... %>
<%# ...some text ... %>
I can see that it enables me to write code in the control/javascript but I don't fully understand the difference between %, %:, %= and %#.
When are they "executed/evaluated"?
Is there a difference if <%= ... => is in a user control or enclosed in single quotes in a Javascript function?
I am not even sure if my title is correct, I don't get anything when googling it.
So I would be delighted with an explanation, more than happy with a link to documentation and happy with the correct terminology.
Assuming you are using the WebForms view engine in ASP.NET MVC all you need to know are the following :
<%= %> - Output the result of the evaluation of the input to the response. For example <%= "<div>foo</div>" %> outputs <div>foo</div>.
<%: %> - Same as the first one except that it HTML encodes the output. Thus <%: "<div>foo</div>" %> will output <div>foo</div>.
<% %> - Evaluates the expression on the server but doesn't output anything in the response. For example you could declare a variable: <% string foo = "foo bar"; %> that you could use later to output with one of the 2 previous methods
<%# %> - This is only used to define the Page or Control directives of the view. It also allows you to bring namespaces and assemblies into scope for the given view. For example <%# Import Namespace="System.IO" %> will bring the System.IO namespace into scope.
As far as <%# is concerned, this is not used in ASP.NET MVC. It is a concept called data binding and works only in classic WebForms applications.
<%= ...some text ... %>: Contains an expression. The result of that expression will have .ToString() called on it, and the resulting string is output directly.
<%: ...some text ... %>: Contains an expression. If the expression result is an IHtmlString, .ToHtmlString() will be called on it, and the results will be output directly. Otherwise, the result of that expression will have .ToString() called on it, and the resulting string is escaped before being output.
<# ...some text ... %>: Contains a page directive for things like namespaces to be included. This is a compiler directive, and is evaluated when the view is getting compiled (usually the first time the view is invoked).
<% ...some text ... %>: Contains one or more statements. These statements are executed.
<%# ...some text ... %>: This is a data-binding expression in ASP.NET WebForms. I don't think it's valid syntax for an MVC View. However, you keep mentioning "user controls", which are not really part of the MVC paradigm, so you might want to reconsider whether this question is really about MVC.
<%# ...some text ... %> is for data-binding expressions; like when you have something like:
<asp:itemtemplate>
<asp:label Text='<%#Eval("Property")%>' .../>
</asp:itemtemplate>
<%= some text %> is basically a shortcut for Response.Write()
<%# some text %> are for application directives; for example when you need to import a namespace: <%# Import namespace="value" %>
I just noticed Stripling warrior answer which compliments this one...
Related
This question already has answers here:
ASP.NET "special" tags
(5 answers)
Closed 10 years ago.
I tried searching SO, but I guess it doesn't like the syntax <%= in the query text so if this is a duplicate, I'll delete it.
My question is, what are the actual names of the following tags? What would i call them if describing them to someone else
<%
<%=
and if you know them
<%#
<%#
<% scriptlets
<%= expression
<%# expression
<%# directives
EDITED
Imagine like this
1. Whatever you write within <% %> are placed in some 'process' function
2. Whatever you write inside <%# %> are placed at class level
3. You can just write expressions inside <%= %> imagine something is written to print Response.Write (we also do not write ';' at the end eg. <%= x+5 %>) and <%# %> is same as <%= %> but the difference is the way they function internally
NOTE: This is just for your understanding and not taken from any source.
<% is a "classic-asp" tag which is used to write server-side code. You can name it scriptlets.You can use it in .net platform in c# also.
<%= is not really a tag. It is used to write something.
%# directives is used for directives
I just wan to to know why these different begin tags exist, if there are still more and if they can be used interchangeably:
<%# Do.Something() %>
<%= Do.Something() %>
...
The tags do different things. For example <%= %> will write the result of the expression within the tags to the output, while <%# %> will use data-binding expressions to reference data from a data source. A fuller explanation can be found at http://weblogs.asp.net/ahmedmoosa/archive/2010/10/06/embedded-code-and-inline-server-tags.aspx
I was using ASP.NET Web Forms and ASP.NET MVC for some period of time.
So <%= %> in views mean Response.Write(), <%: %> introduced in MVC adds html escaping.
In SqlDataSource control designer generates something like this ConnectionString="<%$ ConnectionStrings:FooConnectionString %>" and in repeater you use <%# Eval("") %> sytax.
My question is what exactly do <%$ %> and <%# %> tags, what methods they traslated into and how do they behave?
<%$ %> is the expression syntax.
There are some built in shortcuts for AppSettings, Resources and ConnectionStrings. You can also write your own.
<%# %> is the databinding expression syntax.
This is used in databound controls to resolve property values from the object being bound.
I clipped this text from a book (I can't remember which book) a while ago as I thought it explained the <%# %> syntax well -
Those of you familiar with classic ASP applications might think that
the <%# %> syntax looks very familiar. It is similar in purpose, but
you need to make sure that you don't confuse the two because doing so
could cause your application to function improperly. Whereas in ASP
(and ASP.NET), the <%= %> syntax causes whatever is inside the
brackets to be evaluated at render time, the <%# %> brackets unique to
ASP.NET are evaluated only during binding. As you will see later in
this section, the page and each bindable control on the page have a
DataBind() method. The expressions contained within the data binding
brackets (<%# %>) are evaluated only when the control's DataBind
method is invoked.
That only answers half your question, but Rob Stevenson-Leggett's excellent answer covers everything else. I just thought I'd add this by way of some further illustration.
I have written a blog post about the different types of expressions available in ASP.NET Web Forms:
Expressions vs. Statements, part 2: ASP.NET Code Block Types (internet archive)
It explains the following expression blocks:
<%$ %>
ASP.NET Expression Syntax, used to bind against application settings, connection strings, and resources.
<%# %>
ASP.NET Data-Binding syntax, only evaluated when calling a data binding method of the control.
<% %>
Code blocks, the code becomes part of RenderMethodDelegate. The code should be statements. Use Response.Write to output something.
<%= %>
Same as above, except the code should be a single expression. It will be wrapped in HtmlTextWrite.Write().
In the past I have always worked from the code behind page in ASP.NET and emitting all of my markup from that area.
I am now learning MVC and am working in the .ASPX page directly.
The following statements can be written...
<%= Html.Label("Test") %>
<%: Html.Label("Test")%>
<% Html.Label("Test"); %>
The first two emit the word 'Test', while the 3rd doesn't do anything. Yet no errors are thrown.
I seem to recall <%= is shorthand for <% Response.Write("") %>. Is <%: the same shortcut?
I suppose the 3rd statment runs, but simply returns a string that is discarded.
You are right about 1 and 3. 2 does as one, but it html escapes the output.
I've programmed in both classic ASP and ASP.NET, and I see different tags inside of the markup for server side code.
I've recently come across a good blog on MSDN that goes over the difference between:
<%= (percentage together with equals sign) and
<%# (percent sign and hash/pound/octothorpe)
(<%# is evaluated only at databind, and <%= is evaluated at render), but I also see:
<%$ (percent and dollar sign) and
<%# (percent sign and at symbol).
I believe <%# loads things like assemblies and perhaps <%$ loads things from config files? I'm not too sure.
I was just wondering if anyone could clarify all of this for me and possibly explain why it's important to create so many different tags that seemingly have a similar purpose?
<% %> - is for inline code (especially logic flow)
<%$ %> - is for evaluating expressions (like resource variables)
<%# %> - is for Page directives, registering assemblies, importing namespaces, etc.
<%= %> - is short-hand for Response.Write (discussed here)
<%# %> - is used for data binding expressions.
<%: %> - is short-hand for Response.Write(Server.HTMLEncode()) ASP.net 4.0+
<%#: %> - is used for data binding expressions and is automatically HTMLEncoded.
<%-- --%> - is for server-side comments
You've covered 2 of them (<%# is evaluated only at databind, and <%= is evaluated at render), and the answer for "<%#" is that it's compiler directives (ie., stuff like what you'd put on a compiler's command line).
I don't know about "<%$".