[ASP>NET]<%$, <%@, <%=, <%# …有什麼不同?

  • 811
  • 0

[ASP>NET]<%$, <%@, <%=, <%# …有什麼不同?

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



<%# is a data binding syntax for use in databound controls.
<%= is a short cut for Response.Write.
<%@ is a directive to include a namespace, page directives, etc.



<% %>  An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class. http://msdn.microsoft.com/en-gb/library/ms178135(vs.80).aspx

<%= %> most useful for displaying single pieces of information. http://msdn.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx

<%# %> Data Binding Expression Syntax. http://msdn.microsoft.com/en-us/library/bda9bbfx.aspx

<%$ %> ASP.NET Expression. http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx

<%@ %> Directive Syntax. http://msdn.microsoft.com/en-us/library/xz702w3e(VS.80).aspx

<%-- --%> Server-Side Comments. http://msdn.microsoft.com/en-US/library/4acf8afk.aspx

<%: %> Like <%= %> But HtmlEncodes the output (new with Asp.Net 4). http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx


來源:
<%$, <%@, <%=, <%# … what's the deal?
https://stackoverflow.com/questions/957284/whats-the-deal

官方說明:
ASP.NET data binding overview
https://support.microsoft.com/en-us/help/307860/asp-net-data-binding-overview