Creating a Custom ExpressionBuilder in ASP.NET 2.0
page 2 of 6
by Bilal Haidar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 35127/ 63

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

User Comments

Title: Enquiry   
Name: Bharat Bhushan
Date: 2010-11-03 1:31:44 AM
Comment:
I got the use of expression prefix but i want to know
about type attributes of add tag of expressionbuilders
tag
Title: Expression Builder - Excellent!   
Name: Matt, UK
Date: 2009-10-19 5:48:20 PM
Comment:
Hi,

Fantastic article. Downloaded the sample and it works like a dream. Many thanks for this!
Title: Expression Builder   
Name: Unknown
Date: 2008-12-12 7:24:13 AM
Comment:
Really very good example with all the description in short language

Thanks a lot and It's easy to understand...
Title: Custom Expression   
Name: S.Y.
Date: 2007-09-27 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 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-23 1:46:59 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search