11

Possible Duplicate:
What's the difference between <%# %> and <%= %>?
<%$, <%@, <%=, <%# … what's the deal?

I apologise if this is duplicated, but it's infuriatingly difficult to google for.

Community
  • 1
  • 1
NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • Also http://stackoverflow.com/questions/197047/what-is-the-difference-between-and – Oded Feb 22 '12 at 16:23
  • and http://stackoverflow.com/questions/160097/whats-the-difference-between-and and several others – Eric King Feb 22 '12 at 16:24
  • Cheers, shame you can't search for <% using the search... even the suggestions list didn't have those in. How on earth did you find them? :) I'll close as dupe – NibblyPig Feb 22 '12 at 16:25
  • Visit the questions above that this one duplicates and upvote it. Comment if it helps you at all. Adding a comment might help nudge it up the results list in your favorite search engine. – Joel Etherton Feb 22 '12 at 16:26

2 Answers2

18

<%: is new to .NET 4.0 - it is equivalent to HttpUtility.HtmlEncode(Response.Write()).

<%= is older and stands for Response.Write() only.

<%# is a binding expression.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 3
    Minor correction : Response.Write(HttpUtility.HtmlEncode()) - first encode, then write, not the other way around :) – nsimeonov Feb 13 '18 at 01:59
7

Here's a good article on them. In summary:

Page Directive

<%@ Page Language="C#"  %>

Rendering Code

<% Response.Write("Hello World!");  %>

<%= SayHello("Ahmed") %>

<%: DateTime.Now.ToString()  %>

Expression Syntax

<%$ ConnectionStrings:ConnStrFromWebConfig  %>

<%$ AppSettings:ValueFromWebConfig  %>

<%$ Resources:Resource, Arabic  %>

<%$ RouteValue:year  %>

<%$ YourExpressionPrefix : Any   %>

Data Binding Syntax

<%# Eval("Name")  %>

<%# Bind("Name")  %>

<%# XPath ("Name")  %>

Comment Server

<%-- <asp:Label runat="server" Text="Label"></asp:Label>-- %>
geekchic
  • 1,526
  • 1
  • 11
  • 21