Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

$-Expressions in ASP.NET


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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.