views:

183

answers:

4

I saw an example of using Expression Builders, and creating your own Custom Expression Builder Classes here:

http://aspnet.4guysfromrolla.com/articles/022509-1.aspx

However, I fail to see the value in using this approach. It doesn't seem much easier than programmatically setting values in your code behind.

As far as I can tell, the only thing you can do with them is set properties. Maybe they would be useful for setting defaults on certain controls?

Can anyone shed light on where this ASP.NET feature becomes powerful?

+2  A: 

We are using a custom expression builder to localize our application. E.g. the markup looks like this:

<asp:LinkButton Text="<%$ Str:SomeString %>" ... />

The expression builder reads a string with the ID SomeString from a resource file (taking into account the current user's language preferences) and assigns it to the Text property of the LinkButton.

This is quite flexible: we can add languages by simply copying a resource file into the application directory. And if a customer wants to have a different text for that linkbutton, he just adds his custom string to the resource files and changes the string-ID in the expression builder (without having to change the code-behind).

M4N
I was thinking about that, but what about text that isn't contained within an ASP.NET control's property?
SkippyFire
It only works with control properties. But you can always use a asp:literal control to display some text.
M4N
Martin, how does this differ from using the built in ResourceExpressionBuilder?
RichardOD
There's not really a difference, except that we have our resource files in our own format (which can easily be edited/translated/extended by customers).
M4N
So do you create an <ASP:Literal> tag (or other control) for EVERY piece of localized text?
SkippyFire
Yes! But most of the localized texts are links or labels or grid header columns anyway. In addition, even if you put some plain text into an ASPX page, it will be converted to a literal control during compilation of the markup.
M4N
Does ResourceExpressionBuilder expect the Resource file to be in the Web Application project? ie if you have the Resource file in a common class library project accessed by web and service layer you would use a custom expression builder.
Paul Rowland
+1  A: 

Custom Expressions are handy if you care about ViewState (you should). See TRULY Understanding ViewState.

Pavel Chuchuva
A: 

Making some client side javascript parameters "dynamic", is a good use for this feature.

So say you have a setting in a web.config file that you want to make its way down to a client in a javascript tag. You could handle the OnRender event in code behind and muck around with the js there but that would be ugly. Much nicer to do something like this in the ASPX:

 <script type="text/javascript">
   var sessionKill = <%$ AppSettings:ClientSessionTimeOut%>
Mark Arnott
A: 

It is useful when you need the expression to execute early in the page life cycle. It executes when the parameter is needed not at a particular point in the page life cycle.

Also have a look at making a general purpose 'Code' expression builder.

Myster