Creating a Custom ExpressionBuilder in ASP.NET 2.0
page 2 of 6
by Bilal Haidar
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 14415/ 226

Built-in ExpressionBuilders

ASP.NET 2.0 ships with three ExpressionBuilders:

AppSettingsExpressionBuilder: Used to retrieve values from the AppSettings section of the web configuration file.

ConnectionStringsExpressionBuilder: Used to retrieve values from the ConnectionStrings section of the web configuration file.

ResourceExpressionBuilder: Used to retrieve values from the resource files, whether Local or Global resources.

AppSettingsExpressionBiulder

The .NET Framework creates a new instance of the AppSettingsExpressionBuilder when you are using a declarative expression that is accessing a key from the AppSettings section of the web configuration file.

The example clarifies how to access the AppSettings section declaratively.

Listing 2

<asp:Label ID="lblPath" runat="server" Text='<%$ AppSettings: WebPath %>' />

As explained above, the code is retrieving the WebPath key from the AppSettings section and binding it to the Text property of the Label server control.

ConnectionStringsExpressionBuilder

Most of you have seen such a code:

Listing 3

<asp:SqlDataSource ID="SqlDataSource1"
 runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
SelectCommand="SELECT * FROM [CMS_ACL]"></asp:SqlDataSource>

This is a SqlDataSource control that handles the connection with a database and retrieves data according to the SelectCommand. You can see the existence of the ConnectionString property that has been assigned a declarative expression to retrieve the connection string from the ConnectionStrings section of the web configuration file.

ResourceExpressionBuilder

ASP.NET 2.0 made building Multilanguage web applications an easy task to do when they introduced the App_LocalResources and App_GlobalResources special folders to hold the local and global resource files respectively. Accessing both Global and Local resources can be done through the explicit declarative expression by specifying the Resources keyword on the left hand side of the declarative expression.

We have created a local resource file called Default.aspx.resx placed in the App_LocalResources folder. In a web form named Default.aspx we have placed the following Label server control.

Listing 4

<asp:Label ID="lblResourceKey" runat="server" Text="<%$ Resources:KeyFromResource %>" />

As you can see, the keyword Resources have been used on the left hand side of the expression and on the right hand side the key name to retrieve its values has been specified.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 5 and 2 and type the answer here:

User Comments

Title: Custom Expression   
Name: S.Y.
Date: 9/27/2007 4:34:17 PM
Comment:
The custom expression works great but is there a way to set this from the properties expression editor? It only accepts RESOURCES, APPSETTINGS and CONNECTIONSTRINGS. Is it possible to have the XmlSettings part of the expression type? Wanted to view the actual literal at the design time.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 10/7/2008 1:13:52 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search