31

I have come across different symbols in .aspx page of asp.net

<%#eval(expr) %> 
<%#bind(expr) %>
<% %>  - for specifying the c# code in aspx page
<%$ %> - for specifying the SQL connection string in <asp:SqlDataSource>

Is there any underlying logic behind these symbols or is it just syntax which we have to remember blindly? What does <% %> mean in general?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Sreedhar Danturthi
  • 7,119
  • 19
  • 68
  • 111

2 Answers2

45

It is just syntax.

<% %> is simply short for <script runat="server"> </script> aka code render blocks.

<%# %> are binding expressions (plus the above).

<%= %> is the above + a Response.Write().

<%: %> is the above + a Response.Write() wrapped in Html.Encode (new in .NET 4.0).

<%$ %> is an ASP.NET expression, used to bind configuration or resource file data during runtime.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    Thanks for your response, could you cite any references on this information? I have tried to find this in documentation many times, with no results. Also, what about <%$ %> ? – pseudocoder Jun 02 '11 at 17:04
  • @pseudocoder - Follow the links. I don't know about `<%$ %>`. – Oded Jun 02 '11 at 18:08
  • I did some digging based of your initial links and I found out that `<%$ %>` is called an [ASP.NET Expression](http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx) and allows you to pull values from configuration or resource files. – pseudocoder Jun 02 '11 at 19:15
  • <% ... %> is not simply short for – thorn0 Sep 18 '12 at 21:21
  • 1
    Another construct is `<%-- .. --%>` for comment blocks (that won't turn up in the resulting HTML). – Mr Lister Oct 10 '14 at 07:58
  • 6
    I realise this is nearly 5 years later, but might be worth adding to your list the `<%#: %>` notation... which is the HTML encoded version of the binding expression that was [added as part of ASP.NET 4.5](http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097391) – freefaller Jan 15 '16 at 15:53
2

Remember it. It's more poorly documented ASP.net syntax to help maintain inconsistencies and fallout from ASP 'classic' groans. Also don't forget

<%=variablename %>  
Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91