inline tags... sorting them all out (<%$, <%=, <%, <%#, etc.)

There are all sorts of different inline tags, and I haven't found a place that explains them all in one place, so here is the quick and dirty...

*UDPATED 2012-02-22: Thanks to Conrad Buck and Austin for updates!

<% ... %>

The most basic inline tag, basically runs normal code: 

<% if (User.IsInRole("admin")) { %>
    You can see this
<% } else { %>
    You are no admin fool!
<%} %>



<%= ... %>

Used for small chunks of information, usually from objects and single pieces of information like a single string or int variable: 

The Date is now <%= DateTime.Now.ToShortDateString() %>
The value of string1 is <%= string1 %>

*note: <%= is the equivalent of Response.Write() -  Courtesy of Adam from the US,thanks!



<%# .. %>

Used for Binding Expressions; such as Eval and Bind, most often found in data controls like GridView, Repeater, etc.:

<asp:Repeater ID="rptMeetings" DataSourceID="meetings" 
        <%# Eval("MeetingName")%>



<%$ ... %>

Used for expressions, not code; often seen with DataSources: 

<asp:SqlDataSource ID="party" runat="server" 
    ConnectionString="<%$ ConnectionStrings:letsParty %>" 
    SelectCommand="SELECT * FROM [table]"



<%@ ... %>

This is for directive syntax; basically the stuff you see at the top your your aspx pages like control registration and page declaration: 

<%@ Page Language="C#" 
    AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="_Default" Title="Untitled Page" %>
<%@ Register TagPrefix="wp" Namespace="CustomWebParts" %>



<%-- ... --%>

This is a server side comment, stuff you don't want anyone without code access to see:

<asp:Label ID="lblAwesome" runat="server"/>
<%-- sometimes end users make me angry --%>
<asp:LinkButton ID="lbEdit" Text="Edit" OnClick="Edit_Click" runat="server" />


<%: ... %>

This tag is identical to the "<%= ... %>" tag except that it auto-html-encodes the value within the tag. As Phil Haack said: I often tell people it’s <%= but with the = seen from the front.:

<%: Html.TextBox("FirstName") %>


And that's that.  

kick it on

Comments (15) -

  • Great summary. You are missing one percent sign % in the closing tag of the comment sample ;)
  • Thanks for putting these all together I was wonder about this just last week.
  • <%= is the equivalent of Response.Write()
    just a little extra tidbit of information Smile
  • Sam
    I was very happy to discover the server side comments. I don't want visitors to see my snarky comments Smile
  • Jay
    I was just thinking of putting something like this together for my reference. Thanks for taking the time to do it!
  • Thanks for all the input!
  • Gr8 work!!
  • Thanks, it helped us a lot.
  • ra
    Thanks a lot...

    rajar india
  • Ven
    Great Summary!Very helpful!
  • Bookmarking this... thanks.
  • This is great! Thanks.
  • Thanks for putting these all together I was wonder about this just last week.
  • Thank you  so much for the great work that you have done
  • I have read your article and i really liked it. I have read the bullet points and i completely agree with you. Thank you for sharing your thoughts.
Comments are closed