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
- <%$ ConnectionStrings:DefaultConnection %>
Let’s lists all ASP.NET Expression and it’s task:
- 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" %>
- 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….
- 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") %>"/>
- 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.
-
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 %>
-
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 –%>
- It’s to comment a block of code so that it’ll not rendered or executed inside the page.
- 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.







