ASP.NET Expressions

ASP.NET Expression is written inline surrounded with <% %> in other words you will write them with your markup in .ASPX files for specific task, for example if want to access a connection string there an expression for that

  1. <%$ ConnectionStrings:DefaultConnection %>

Let’s lists all ASP.NET Expression and it’s task:

  1. Directive Expression <%@ .. %> 
    • It’s used in Web Forms .ASPX files or User Control .ASCX files to set settings, for example the Page directive where you can declare Title, Master Page, Language, etc….
      <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPNETExpressions._Default" %>

  2. Data-Binding Expression <%# .. %>
    • Create binding between server control with data source when calling DataBind() method, most of the time you’ll see this expression inside Data Bound Controls like GridView and DetailView.
      <asp:TextBox runat="server" ID="txtFirstName"  Text="<%# Eval("FirstName") %>"/>

  3. Expression Builder <%$ .. %>

    • This expression is used to set controls properties that located in configuration files Web.Config such as AppSettings, ConnectionStrings, or Resources
    • It’s syntax is <%$ Expression Prefix: Expression Value %>
      <%$ ConnectionStrings:DefaultConnection %>

  4. Server-side Comment Expression <%– .. –%>

    • It’s to comment a block of code so that it’ll not rendered or executed inside the page.
      <%– This is a comment –%>

  5. Displaying Expression <%= .. %> 
    • It’s the simplest way to display one piece of information such as Integer or String inside the page, it’ll be converted to Response.Write().
      <%= DateTime.UtcNow.ToString("dd/MM/yyyy") %>

      Tip:

      Every time you’ll see <% look at the next character:

      • If it @ then it’s Directive Expression.
      • If it = then it’s Displaying Expression.
      • If it # then it’s Data-Binding Expression.
      • If it — then it’s Comment.
      • If it $ then it’s Expression Builder.

      Note:

      If you make incorrect syntax inside these expression, Exception will be thrown.

      Microsoft Support: Introduction to ASP.NET inline expressions in the .NET Framework

      Tagged on: ,
      • http://twitter.com/HosamKamel HosamKamel

        Nice post Emad!

      • EmadMokhtar

        Thanks Hosam

      • http://twitter.com/waelouf Wael Refaat ◕‿◕

        you can add also:

        ASP.NET inline server side code

        (equals except it will encode values inside the tag)
        html encoding

        a great post :)

      • Mohamed_Meligy

        Regarding Expression builder, in the days I have done webforms in SilverKey, I used to abuse the expressions. That was my secret weapon, because ASP.NET wouldn’t let me do on server side controls, and the only other way was using data binding for everything to get , which had several bad side effects.

        worked very nicely, except that it didn’t have intellisense support. When I really needed it, I’d write some or somewhere else, copy the code, then delete it.

        We learned about it from:
        http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx

        While searching for this link again, I found a collection of expression builders including the code one and few more:
        http://aspnet.4guysfromrolla.com/articles/030409-1.aspx

        Helps a bit with webforms :)