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?

Answers


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

viewMenuItems doesn't appear when applying skin to ViewNavigatorApplication

flash flex flash-builder

ViewMenuItems works fine when I don't apply skinClass fo ViewNavigatorApplication, but when I apply custom skin to ViewNavigator, ViewMenuItems doesn't appear.

date as SEO optimization tool

php html date seo

Consider the following PHP code for printing current date:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.