You are currently offline, waiting for your internet to reconnect

Introduction to ASP.NET inline expressions in the .NET Framework

This article contains an introduction to the following ASP.NET inline expressions:
  • <% ... %>
  • <%= ... %>
  • <%@ ... %>
  • <%# ... %>
  • <%$ ... %>
  • <%-- ... %>
  • <%@ ... %>

<% ... %> embedded code blocks

The embedded code block is used to preserve backward compatibility with classical ASP. The code in the block can execute programming statements and call functions in the current page class during the page-rendering phase.

The following example demonstrates an ASP.NET page that has sample Microsoft Visual Basic .NET code in an embedded code block to display the results of a loop:
<%@ Page Language="VB" %><html><body>    <form id="form1" runat="server">    <% For i As Integer = 16 To 24 Step 2%>    <div style="font-size: <% Response.Write(i)%>">        Hello World<br />    </div>    <% Next%>    </form></body></html>
Because an embedded code block is always mixed with the HTML source, it is difficult for developers to read and maintain them.

For more information about embedded code blocks in ASP.NET Web pages, visit the following Microsoft Developer Network (MSDN) Web site:

<%= ... %> displaying expression

The <%= ... %>displaying expression is an equivalent of the embedded code block that contains only the Response.Write(…) statement. This is the simplest way to display information such as a single string, an int variable, or a constant.

For example, the following sample code displays the current time:
<%@ Page Language="VB" %><html><body>    <form id="form1" runat="server">    <%=DateTime.Now.ToString() %>    </form></body></html>
Remember that the displaying expression cannot be used in the attributes of server controls. This is because the .NET Framework directly compiles the whole expression instead of the displaying content as the value to the attribute.

For more information about how to display information from ASP.NET, visit the following MSDN Web site:

<%@ ... %> directive expression

The directive expression is the syntax that specifies settings that are used by the page and by user control compilers when they process ASP.NET Web Form (.aspx) pages and User Control (.ascx) files.

The ASP.NET page framework supports the following directives:
@ PageDefines page-specific attributes that are used by the ASP.NET page parser and compiler. Can be included only in .aspx files.
*This directive name can be used only in ASP.NET Web Form pages.
@ ControlDefines control-specific attributes that are used by the ASP.NET page parser and compiler. Can be included only in .ascx files (user controls).
*This directive name can be used only in User Control files.
@ ImportExplicitly imports a namespace into a page or into a user control.
@ ImplementsDeclaratively indicates that a page or a user control implements a specified .NET Framework interface.
@ RegisterAssociates aliases with namespaces and with class names. This enables user controls and custom server controls to be rendered when they are included in a requested page or user control.
@ AssemblyLinks an assembly to the current page during compilation. It makes all the assembly's classes and interfaces available for use on the page.
@ MasterIdentifies an ASP.NET master page.
@ WebHandlerIdentifies an ASP.NET IHttpHandler page.
@ PreviousPageTypeProvides a way to obtain strong typing against the previous page as accessed through the PreviousPage property.
@ MasterTypeAssigns a class name to the Master property of an ASP.NET page. Provides a way to create a strongly typed reference to the ASP.NET master page.
@ OutputCacheDeclaratively controls the output caching policies of a page or of a user control.
@ ReferenceDeclaratively links a page or user control to the current page or user control.
For more information about directive syntax, visit the following MSDN Web site:

<%# ... %> data-binding expression

The data-binding expression creates binding between a server control property and a data source when the control’s DataBind method of this server control is called on the page.

The following example shows how to use the data-binding expression to bind the string from a function to the Text property of a label:
<%@ Page Language="VB" %><script runat="server">    Protected Function SayHello() As String        Return "Hello World"    End Function    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)        lblHello.DataBind()    End Sub</script><html><body>    <form id="form1" runat="server">    <asp:Label ID="lblHello" runat="server" Text="<%# SayHello%>"></asp:Label>    </form></body></html>
For more information about data-binding expression syntax, visit the following MSDN Web site:

<%$ ... %> expression builder

The expression builder is used to set values of control properties based on the information that is contained in an application's configuration or resource files. The following is the basic syntax of the expression builder:
<%$ Expression Prefix: Expression Value %>
The dollar sign ($) indicates to ASP.NET that the following expression is an expression builder. The expression prefix defines the kind of expression, such as AppSettings, ConnectionStrings, or Resources. Additionally, you can create and define your own expression builder. The expression value that follows the colon (:) is what ASP.NET will actually use as the value of a certain property.

The following demo shows how to use the expression builder to obtain the copyright of a Web site from the AppSettings node in the Web.config file and how to then set the copyright information as the value of the Literal’s Text property.

The AppSettings node in Web.config file:
<appSettings>    <add key="copyright" value="(c) Copyright 2009"/></appSettings>
The expression builder in the ASP.NET Web Form page:
<div id="footer">    <asp:Literal ID="Literal1" runat="server" Text="<%$ AppSettings: copyright %>"></asp:Literal></div>
For more information about ASP.NET expressions, visit the following MSDN Web site:

<%-- ... -- %> server-side comments block

The server-side comments block lets developers embed code comments in any location of the HTML source of ASP.NET Web Form pages (except for within <script> code blocks). Any content between the opening and closing tags of the server-side comments block will not be processed on the server or rendered on the resulting page.

The following code example shows how to use the server-side comments block in an ASP.NET page:
<%@ Page Language="VB" %><script runat="server">    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)        Dim strName As String        strName = Session("userName")        lblUserName.Text = strName    End Sub</script><html><body>    <form id="form1" runat="server">    <%-- Label for UserName --%>    <asp:Label ID="lblUserName" runat="server" Text=""></asp:Label>    </form></body></html>
For more information about server-side comments, visit the following MSDN Web site:

Article ID: 976112 - Last Review: 10/20/2009 04:57:50 - Revision: 2.0

  • Microsoft .NET Framework 2.0
  • Microsoft .NET Framework 3.0
  • Microsoft .NET Framework 3.5
  • kbsurveynew kbinfo kbhowto kbexpertiseadvanced KB976112