简体   繁体   中英

When are ASP.NET Expression Builders most useful?

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

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

We are using a custom expression builder to localize our application. Eg 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).

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

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 .

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%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM