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

A Brief History of Expressions in ASP.NET


There are various expression types in ASP.NET, each serving a particular scenario and providing a specific capability. An ASP.NET expression is any executable piece of code you can write in the .aspx file wrapped by <% ... %> tags. Introduced with classic ASP, <%...%> code blocks indicate code to execute in place with the results to be incorporated locally in the page response. ASP-style code blocks, though, were designed for an interpreted environment like ASP, and don't easily fit in the compiler-based and object-oriented world of ASP.NET.

For this reason, ASP.NET 1.x supports data binding expressions, characterized by <%# ... %>. You can use data binding expressions to assign calculated values to control properties in a declarative manner. The drawback of these expressions is that they require an explicit data binding step to kick in and do their own job. In other words, each control that uses a similar expression must have the DataBind method called to trigger the process that will assign a value to the expression. Aside from the need of calling the DataBind method in the Page_Load event handler, this model works just fine in ASP.NET 1.x and to some extent also in ASP.NET 2.0.

In ASP.NET 2.0, pages using data source controls can't properly leverage data binding expressions to set properties declaratively. Let's briefly consider a page that incorporates a SqlDataSource control and a GridView, exactly in this order. If you use a data binding expression to, say, retrieve the connection string from a separate storage medium (i.e., the web.config file) and you call DataBind, all works just fine. Being the first control in the page, the SqlDataSource is fully initialized when the GridView enters its initialization stage and can provide data as needed. What if you revert the order of the controls in the page and move the GridView up in the hierarchy?


<asp:GridView id="grid" runat="server"
   datasourceid="SqlDataSource1" />
<asp:SqlDataSource id="SqlDataSource1" runat="server"
   ConnectionString=
       <% #ConfigurationManager.ConnectionStrings[...]
%>
   :
/>

You would reasonably expect that the page work as usual. But, interestingly enough, this is not what happens. Let's see why.

The page fails because the connection string is not set yet when the grid needs it during its own initialization step. The GridView control, as well as other new data-bound controls such as DetailsView and FormView, start their binding process as soon as the DataSourceID property is set. In the preceding code, the DataSourceID property is set declaratively meaning that the binding process is started at initialization time. The grid receives its data from the SqlDataSource1; but this control has not yet been fully initialized and its connection string property is not set at this time. (No data-binding event has been triggered yet.) Hence, the runtime error.

In ASP.NET 2.0, dynamic $-expressions serve the purpose of setting control properties declaratively without relying on the data binding mechanism and without incurring in the unfortunate situation described above. A $-expression is evaluated in place and during the initialization of the control care of the internal method that builds the control from the source ASPX markup. Here's how to rewrite the preceding code to make it work with $-expressions:


<asp:GridView id="grid" runat="server" 
   datasourceid="SqlDataSource1" />
<asp:SqlDataSource id="SqlDataSource1" runat="server"
   ConnectionString=<% $ConnectionStrings:[...] %>
   :
/>

To declaratively bind a control property to the value of the expression you follow the schema below:


<%$ Prefix:Expression %>

The prefix refers to the type of the expressions whereas the expression is the body of the expression. ASP.NET 2.0 supports three types of expressions: ConnectionStrings, AppSettings, and Resources. They respectively retrieve data from the <connectionStrings> and <appSettings> section of the web.config file and from the application resources. The expression contains detailed information to locate the piece of data to retrieve. For example, for connection strings, it points to the name of the entry in the section where the physical connection string is stored.


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.