简体   繁体   English

ASP.NET 表达式生成器什么时候最有用?

[英]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 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?任何人都可以阐明这个 ASP.NET 功能在哪里变得强大吗?

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.表达式生成器从资源文件中读取 ID 为 SomeString 的字符串(考虑到当前用户的语言首选项)并将其分配给 LinkButton 的 Text 属性。

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).如果客户希望该链接按钮有不同的文本,他只需将自定义字符串添加到资源文件并更改表达式构建器中的字符串 ID(无需更改代码隐藏)。

Custom Expressions are handy if you care about ViewState (you should).如果您关心 ViewState(您应该),自定义表达式会很方便。 See TRULY Understanding ViewState .请参阅真正了解 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.使一些客户端 javascript 参数“动态”,可以很好地利用此功能。

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.假设您在 web.config 文件中有一个设置,您希望通过 javascript 标记将其传递给客户端。 You could handle the OnRender event in code behind and muck around with the js there but that would be ugly.您可以在代码隐藏中处理 OnRender 事件,并在那里处理 js,但这会很丑陋。 Much nicer to do something like this in the ASPX:在 ASPX 中做这样的事情要好得多:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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