Asp.Net - what is <%$? - asp.net

I should know this by now, but I don't, and for some reason, I am not finding the answer on Google, so I thought I'd try here.
I know that <%= %> is the equivalent of Response.Write()
And I've seen <%# %> for databinding.
However, today I noticed something new, and even though I can see what it's doing, I am looking for the official documentation on this. In one of my web pages, I see
ConnectionString="<%$ ConnectionStrings:SomeConnectionString %>"
So what does <%$ %> do?

See this question:
In ASP.Net, what is the difference between <%= and <%#
In summary ,there are a several different 'bee-stings':
<%# - Page/Control/Import/Register directive
<%$ - Resource access and Expression building
<%= - Explicit output to page, equivalent to <% Response.Write( ) %>
<%# - Data Binding. It can only used where databinding is supported, or at the page level if you call Page.DataBind() in your code-behind.
<%-- - Server-side comment block
<%: - Equivalent to <%=, but it also HTMLEncode()s the output.

Used for expressions, not code; often seen with DataSources
http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx

It's markup used to evaluate expressions rather than code.

Related

What are these <%$ %> asp.net markup tags called?

I was reading about how to work with SqlDataSource objects by adding properties in the markup, and for the ConnectionString property, the author used something like
<asp:SqlDataSource ... ConnectionString="<%$ ConnectionStrings:lyric %>" ... >
The use of <%$ %> is new to me and the author didn't really explain it. I mean, I can see pretty easily what it does in this instance.
But, I want to learn the syntax inside <%$ %> to access other things inside Web Config. I don't know what this type of markup is called so I can't google it. I'm having a rough time finding it.
Do tags like <%# %>, <%# %>, and <%$ %> have a special blanket name? (so I can learn other variations)
Is there a specific name for <%$ %>? (so I can look it up and learn the syntax)
Thanks in advance
They are called "ASP.NET inline expressions" or "ASP.NET inline code-blocks". You can learn more about them here: http://support.microsoft.com/kb/976112
<%$ ... %> is the syntax for an expression builder.

ASP.net: What is the difference between <%, <%= and <%#? [duplicate]

I have been using ASP.NET for years, but I can never remember when using the # and = are appropriate.
For example:
<%= Grid.ClientID %>
or
<%# Eval("FullName")%>
Can someone explain when each should be used so I can keep it straight in my mind? Is # only used in controls that support databinding?
There are a couple of different 'bee-stings':
<%# - page directive
<%$ - resource access
<%= - explicit output to page
<%# - data binding
<%-- - server side comment block
Also new in ASP.Net 4:
<%: - writes out to the page, but with HTML encoded
Also new in ASP.Net 4.5:
<%#: - HTML encoded data binding
<%= %> is the equivalent of doing Response.Write("") wherever you place it.
<%# %> is for Databinding and can only be used where databinding is supported (you can use these on the page-level outside a control if you call Page.DataBind() in your codebehind)
Databinding Expressions Overview
Here's a great blog post by Dan Crevier that walks through a test app he wrote to show the differences.
In essence:
The <%= expressions are evaluated at render time
The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
<%# expressions can be used as properties in server-side controls. <%= expressions cannot.

How does <%$ %> and <%# %> work in ASP.NET?

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().

<%$, <%#, <%=, <%# ... what's the deal?

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 "<%$".

When should I use # and = in ASP.NET controls?

I have been using ASP.NET for years, but I can never remember when using the # and = are appropriate.
For example:
<%= Grid.ClientID %>
or
<%# Eval("FullName")%>
Can someone explain when each should be used so I can keep it straight in my mind? Is # only used in controls that support databinding?
There are a couple of different 'bee-stings':
<%# - page directive
<%$ - resource access
<%= - explicit output to page
<%# - data binding
<%-- - server side comment block
Also new in ASP.Net 4:
<%: - writes out to the page, but with HTML encoded
Also new in ASP.Net 4.5:
<%#: - HTML encoded data binding
<%= %> is the equivalent of doing Response.Write("") wherever you place it.
<%# %> is for Databinding and can only be used where databinding is supported (you can use these on the page-level outside a control if you call Page.DataBind() in your codebehind)
Databinding Expressions Overview
Here's a great blog post by Dan Crevier that walks through a test app he wrote to show the differences.
In essence:
The <%= expressions are evaluated at render time
The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
<%# expressions can be used as properties in server-side controls. <%= expressions cannot.

Resources