Partager via


ASP.NET Expressions Overview

ASP.NET expressions are a declarative way set control properties based on information that is evaluated at run time. For example, you can use expressions to set a property to values that are based on connection strings, application settings, and other values contained within an application's configuration and resource files. Expressions are evaluated at run time when the declarative elements of the page are parsed, and the value represented by the expression is substituted for the expression syntax. (Because expressions are evaluated at parse time, you cannot dynamically create expressions in code.)

A common use of expressions is in data source controls to reference a connection string. Rather than including the connection string directly in the data source control as a property value, you can use an expression that specifies where the connection string is in the configuration file. At run time, the expression is resolved by reading the connection string from the configuration file. You can use expressions for any property setting that you want to resolve at run time rather than set as a static value.

Using expressions helps you maintain your application in these ways:

  • You can reduce the code in your application by referencing dynamic information declaratively. This avoids having to write code to set property values at run time.

  • You can reuse the same settings in multiple controls. For example, if you store a connection string in the Web.config file and then use expressions to reference the connection string in pages, you change the connection string in one central location rather than changing it in each control that uses the connection string.

  • You can store sensitive data such as connection string credentials (or other application data) in the Web.config file and then encrypt the data.

  • If you are working with a visual designer such as Visual Studio, you can take advantage of tools in the designer that can help you build expressions.

Expressions are also extensible, so that you can define your own expression syntax. You can then create expressions that call your custom expression handler to return a value using your custom logic.

Basic Syntax

The basic syntax of an ASP.NET expression is the following:

<%$ expressionPrefix: expressionValue %>

The dollar sign ($) indicates to ASP.NET that an expression follows. The expression prefix defines the type of expression, such as AppSettings, ConnectionStrings, or Resources. Following the colon (:) is the actual expression value that ASP.NET will resolve.

Expression syntax is not bound to any specific .NET language. You can use the same expression syntax whether you use Visual Basic, C#, or any other programming language in your ASP.NET pages.

Connection Strings

A common use of expressions is to set the connection string property of a control, such as the SqlDataSource control, based on the value of a connection string stored in the Web.config file. For example, you might have the following SqlDataSource control with a connection string attribute:

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" 
    SelectCommand="SELECT * FROM [Employees]"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>">
</asp:SqlDataSource>

The highlighted code shows an expression, which is contained within the quotation marks and denotes the attribute's value. The expression references a connection string named "NorthwindConnectionString1" that is defined in the connectionStrings element of the Web.config file. The connectionStrings element might look like the following:

<configuration>
  <connectionStrings>
    <add name="NorthwindConnectionString1" 
      connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
    <!-- additional settings -->
</configuration>

Note

It is recommended for added security that you encrypt the section of the configuration file that contains connection strings. For details, see Encrypting Configuration Information Using Protected Configuration. In the preceding example, the configuration section of the configuration file is shown unencrypted for clarity.

Each connection string is given a name, which you can use in an expression to reference it within your ASP.NET pages.

Application Settings

In addition to using expressions for connection strings, you can use expressions to reference application settings defined in a configuration file accessible to the Web site. For example, you might have frequently used strings, such as your site's copyright message, stored in the appSettings section of your Web.config file, which might look like the following:

<appSettings>
  <add key="copyright" value="(c)Copyright 2004 Northwind Traders"/>
</appSettings>

Within your ASP.NET pages, you could reference the value by using an expression similar to the following:

<%$ AppSettings: copyright %>

This would enable you to maintain frequently cited elements within the configuration file rather than having to change the same text on every page.

Displaying Static Content Using Expressions

If you want to use an expression as a static value on your page or control, you use an expression as part of an ASP.NET server control. A typical strategy is to add a Literal control and set its Text property to an expression. For example, to place a copyright notice at the bottom of every page you could use the following:

<p align="center">

<asp:Literal runat="server" text="<%$ AppSettings: copyright %>"/>

</p>

Resource Files

In addition to displaying values contained in a configuration file, you can display values that are stored in resource (.resx or .resource) files. You typically use resource files to store information for a specific language or language and culture combination. By using resource files and expressions, you can create a Web page that displays text in different languages depending on values determined at run time, such as the language and culture setting reported by the browser, or an explicit language choice by the user.

For example, when localizing content automatically, you can set the Text property of a server control using expression syntax, as in this example:

<asp:Label id="label1" runat="server" text="<%$ Resources: Messages, ThankYouLabel %>" />

In the App_GlobalResources folder, you could have resource files named Messages.resx, Messages.es.resx, Message.de.resx, and so on—a Messages resource file for each language you want to support. The Messages.resx file represents the neutral (fallback) resource that is used if no explicit culture is set. In the Messages.resx file, you might have an entry like this:

<data name="ThankYouLabel"><value>Thank you very much!</value></data>

You would have similar entries in other resource files, with the strings translated appropriately for the language represented by that resource file. For more information on using expressions for resources and localization, see ASP.NET Web Page Resources Overview.

See Also

Concepts

ASP.NET Web Forms Page Syntax Overview

Other Resources

ASP.NET Configuration File Syntax