The main purpose of ASP.NET expressions is to help developers to set control properties declaratively. Expressions, though, are not the only way to set properties on controls and, in particular, data source controls. You can also use hard-coded strings or, better yet, set properties programmatically in the Page_Load event.
A new breed of expressions is required to do some good declarative programming not just with data source controls but also any other server control in ASP.NET pages. These expressions are known as ASP.NET $-expressions.
ASP.NET 2.0 counts three different types of $-expressions, characterized by a unique prefix and syntax. The prefixes are: $AppSettings, $ConnectionStrings, and $Resources. $AppSettings returns the value of the specified setting from the <appSettings> section of the configuration file. $ConnectionStrings returns the value of the specified attribute of the given entry in the <connectionStrings> section of the configuration file. Finally, $Resources returns the value of the specified global resource. A $-expression is declared as follows:
<%$ Prefix:Expression %>
The prefix indicates the type of the expression whereas the expression contains input data. The $ConnectionStrings builder retrieves a connection string entry from the web.config configuration file. The prefix is ConnectionStrings; the syntax is a dot-separated list of two attributes--the name of the entry in <connectionStrings> section to process and the name of the attribute to read. Here's an example:
<%$ ConnectionStrings:NorthWind.ConnectionString %>
The expression returns the connection string stored in the NorthWind entry in the <connectionStrings>. The part following the dot symbol is optional. If not specified, it defaults to ConnectionString. The preceding code can be rewritten in the more compact format below:
<%$ ConnectionStrings:NorthWind %>
Only two attributes are supported--ConnectionString and ProviderName. The ProviderName attribute indicates the name of the managed ADO.NET provider to which the connection string applies.
The $AppSettings expression provides a declarative way to access the contents of the <appSettings> section in the web.config file. The syntax of the expression consists of a simple string to indicate the name of the entry from which the value has to be read. Consider the following web.config section:
<appSettings> <add key="AppVersionNumber" value="1.1.001" /> </appSettings>
The following expression will return the contents of the value attribute.
<asp:Label runat="server" id="Label1" Text="<%$ AppSettings:AppVersionNumber %>" : />
The $Resources expression retrieves global resources defined in the specified .resx file.
<asp:Label runat="server" id="Label1" Text="<%$ Resources:MyResources, AppTitle %>" : />
The syntax is a comma-separated string where the first element indicates the name of the .resx file to access. The name of the file is without path and extension. The second element specifies the name of the resource to retrieve. Both elements are required.
The blanks in the expressions are generally optional and don't influence the parser. The preceding code retrieves the value of a resource named AppTitle from the MyResources.resx file. Be aware that the $Resources expression builder doesn't retrieve resources local to a page; it only works with .resx files located in the App_GlobalResources folder.
It is important to note that when using expressions to set the value of control properties compatible types must be used. If you bind, say, a connection string where a date is expected you get a runtime exception.
You can extend the default set of $-expressions with new ones. A custom $-expression is a class that inherits from ExpressionBuilder and overrides a few methods. You register the new component in the web.config file and assign it the prefix. The code in the class, instead, takes care of parsing and processing the input data.