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.

Need Your Help

Patterns to respond from the service layer

c# design-patterns

I am trying to list all the alternatives to communicate results back from the service layer to the presentation.

How to define dynamic array in Begin Statement with AWK


I want to define array in my BEGIN statement with undefined index number how can I do this in AWK?

Keep phone numbers from being converted to links on Opera Mobile

html mobile opera

Apparently, those guys at Opera have added a nice feature to their mobile version of the browser: They convert things looking like phone numbers into HTML links on webpages (e.g. USD 15.50, which c...