7

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

https://web.archive.org/web/20210513211719/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?

Charles
  • 50,943
  • 13
  • 104
  • 142
John B
  • 20,062
  • 35
  • 120
  • 170
  • Wow, 7 minutes without an answer! That's a new record for me! – John B Jun 16 '09 at 20:23
  • Good question. I can't think of many useful uses of them either. Scott Mitchell did one for Session- http://www.4guysfromrolla.com/articles/022509-1.aspx which I suppose could be useful (can't say I'd ever use it myself mind). – RichardOD Jun 16 '09 at 20:26
  • Sounds like this feature has a very narrow list of real world applications. But it sounds like localization is one of them, as Martin described. – John B Jun 17 '09 at 18:59

4 Answers4

4

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
  • 94,805
  • 45
  • 217
  • 260
  • I was thinking about that, but what about text that isn't contained within an ASP.NET control's property? – John B Jun 16 '09 at 20:27
  • It only works with control properties. But you can always use a asp:literal control to display some text. – M4N Jun 16 '09 at 20:30
  • Martin, how does this differ from using the built in ResourceExpressionBuilder? – RichardOD Jun 16 '09 at 20:38
  • 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 Jun 16 '09 at 20:43
  • So do you create an tag (or other control) for EVERY piece of localized text? – John B Jun 16 '09 at 20:48
  • 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 Jun 16 '09 at 20:55
  • 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 Jun 17 '09 at 00:15
1

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

Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
1

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
  • 17,704
  • 13
  • 64
  • 93
-1

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
  • 1,975
  • 3
  • 17
  • 28