ASP.NET - The complete list of inline expressions

<%@      %> @ Directive expression

Provides an instruction to the compiler:


  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="wwwroot.Index" %>
  

<%      %> % Embedded code block

Statements to execute during the page's render phase:


  <% Response.Write("The time is: " + DateTime.Now.ToShortTimeString()); %>
  

  The time is: 4:10 PM

  <% for(int i=0; i < 5; i++) { %>
      <p> Paragraph number <% Response.Write(i); %> </p>
  <% } %>
  

  <p> Paragraph number 0 </p>
  <p> Paragraph number 1 </p>
  <p> Paragraph number 2 </p>
  <p> Paragraph number 3 </p>
  <p> Paragraph number 4 </p>

<%=      %> %= Display expression

Equivalent to Response.Write:


  <%= "The time is: " + DateTime.Now.ToShortTimeString() %>
  

  The time is: 4:10 PM

  <% for(int i=0; i < 5; i++) { %>
      <p> Paragraph number <%= i %> </p>
  <% } %>
  

  <p> Paragraph number 0 </p>
  <p> Paragraph number 1 </p>
  <p> Paragraph number 2 </p>
  <p> Paragraph number 3 </p>
  <p> Paragraph number 4 </p>

<%:      %> %: Display expression (HTML encoded)

Same as Display expression, except the output is HTML encoded.


  <%: "You searched for: <html> & doctype" %>
  

Generated HTML:


  You searched for: &lt;html&gt; & doctype

Browser output:


  You searched for: <html> & doctype

<%#      %> %# Data-binding expression

Single-value databinding:

TestPage.aspx:


  Hello <%# this.FirstName %>
  

TestPage.aspx.cs:


  namespace wwwroot
  {
      public partial class TestPage : System.Web.UI.Page
      {
          protected string FirstName;
  
          protected void Page_Load(object sender, EventArgs e)
          {
              this.FirstName = Request["FirstName"];
  
              this.DataBind();
          }
      }
  }
  

Browser output for: /TestPage.aspx?FirstName=Richard


  Hello Richard

<%$      %> #$ Expression builder

Returns the value of an expression:

TestPage.aspx:


  Zuga.net. by:
  <asp:literal runat="server" text="<%$ AppSettings: developer %>"></asp:literal>
  

Web.config


  <configuration>
      <appsettings>
          <add key="developer" value="Richard McGrath" />
      </appsettings>
  </configuration>
  

Generated HTML:


  Zuga.net. by: Richard McGrath

<%--      %> %-- Server side comments

For debugging.

Server-side comments will not be sent to the client.


  <asp:panel runat="server">Before</asp:panel>
  <%--
      <asp:label runat="server">Email:</asp:label>
      <asp:textbox runat="server" placeholder="Your email address"></asp:textbox>
  --%>
  <asp:panel runat="server">After</asp:panel>
  

Generated HTML:


  <div>Before</div>
  <div>After</div>

Ads by Google


Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath