设置的TemplateField的HeaderText动态本地化 [英] Set TemplateField HeaderText dynamic for localization
问题描述
我想为我的ASP.NET code创建本地化,但我有问题设置TemplateField的的HeaderText
我有这样的作品
< ASP:的TemplateField的HeaderText =说明>
<&ItemTemplate中GT;
<%#的eval(说明)%GT;
< / ItemTemplate中>
< FooterTemplate>
< ASP:面板ID =Panel5=服务器DefaultButton =EditSubmission>
< ASP:文本框=服务器ID =Submission_DescriptionTxtBox的TextMode =多行
工具提示='<%#GetById(atforbedringsforslag_description_tooltip)%>/> < / ASP:面板>
< / FooterTemplate>
< / ASP:的TemplateField>
但我想改变
< ASP:的TemplateField的HeaderText =说明>
要
< ASP:的TemplateField的HeaderText ='<%#GetById(atforbedringsforslag_description_title)%>'>
但然后我得到
数据绑定前pressions只支持在具有DataBinding事件的对象。 System.Web.UI.WebControls.TemplateField不具有数据绑定事件
块引用>我应该如何设置这个领域?我可以找到一些使用OnRowCreated,但你使用索引号访问字段,然后就很容易犯错误,或者新的字段添加后来就忘了改指数
编辑我的解决办法:
创建自定义的前pression建设者
使用System.Web.Compilation;
使用系统;
使用系统codeDOM。公共类LocalizationEx pressionBuilder:防爆pressionBuilder
{
公共覆盖codeEX pression得到codeEX pression(System.Web.UI.BoundPropertyEntry项,对象parsedData,防爆pressionBuilderContext上下文)
{
codeEX pression [] = inputParams新的codeEX pression [] {新的codePrimitiveEx pression(entry.Ex pression.Trim()),
新的codeTypeOfEx pression(entry.DeclaringType)
新的codePrimitiveEx pression(entry.PropertyInfo.Name)}; //使用指定的输入参数,返回一个codeMethodInvokeEx pression将调用GetRequestedValue方法
返回新的codeMethodInvokeEx pression(新codeTypeReferenceEx pression(this.GetType()),
GetRequestedValue
inputParams);
} 公共静态对象GetRequestedValue(字符串键,键入TARGETTYPE,字符串propertyName的)
{
//如果我们到达这里,没有类型不匹配 - 返回值
返回GetByText(键);
} //占位,直到数据库的构建
公共静态字符串GetByText(字符串文本)
{
返回文本;
}
}添加了preFIX到我的web.config
<&的System.Web GT;
<编译调试=真defaultLanguage =C#targetFramework =4.0>
<前pressionBuilders>
<加上前pression preFIX =localizeByTextTYPE =LocalizationEx pressionBuilder/>
< / EX pressionBuilders>
< /编译>
< /system.web>和现在我可以让我的文字是这样
< ASP:的TemplateField的HeaderText ='<%$ localizeByText:部分文字%GT;'>
解决方案您可以创建自己的自定义
防爆pression生成器
它调用你的GetById
方法。看看下面的链接,一个古老,但很好的文章,解释如何构建一个前pression建设者和如何使用它:http://www.4guysfromrolla.com/articles/022509-1.aspx
当你有一个前pression生成器,使用
&LT使用它;%$
语法。这是从数据绑定语法不同<%#
有关的领域的HeaderText,不允许使用数据绑定语法(不知道为什么,但是这MS怎么做的话)。使用前pression语法是允许的,一旦你有你的自定义的前pression建设者做会奏效。
不要经过我的联系,这是相当大量的文字,但最终使你的页面的前pression商不会花费太多力气......
此外,该网页已经在底部前pression建设者库,笔者做了一个链接。看看他们,也许他们中的一个可能被直接用来解决您的问题(特别是
codeEX pressionBuilder
)。I am trying to create localization for my ASP.NET code, but I have issues setting the TemplateField's HeaderText
I have this that works
<asp:TemplateField HeaderText="Description"> <ItemTemplate> <%# Eval("Description") %> </ItemTemplate> <FooterTemplate> <asp:Panel ID="Panel5" runat="server" DefaultButton="EditSubmission"> <asp:TextBox runat="server" ID="Submission_DescriptionTxtBox" TextMode="MultiLine" ToolTip='<%# GetById("atforbedringsforslag_description_tooltip") %>'/> </asp:Panel> </FooterTemplate> </asp:TemplateField>
But I want to change
<asp:TemplateField HeaderText="Description">
To
<asp:TemplateField HeaderText='<%# GetById("atforbedringsforslag_description_title") %>'>
But then I get
Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.TemplateField does not have a DataBinding event.
How should I set this field? I can find some that uses OnRowCreated, but then you access the fields with an index number, and then it becomes easy to make mistakes or forgot to change indexes if new fields are added later on
EDIT My solution:
Created the custom expression builder
using System.Web.Compilation; using System; using System.CodeDom; public class LocalizationExpressionBuilder : ExpressionBuilder { public override CodeExpression GetCodeExpression(System.Web.UI.BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { CodeExpression[] inputParams = new CodeExpression[] { new CodePrimitiveExpression(entry.Expression.Trim()), new CodeTypeOfExpression(entry.DeclaringType), new CodePrimitiveExpression(entry.PropertyInfo.Name) }; // Return a CodeMethodInvokeExpression that will invoke the GetRequestedValue method using the specified input parameters return new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(this.GetType()), "GetRequestedValue", inputParams); } public static object GetRequestedValue(string key, Type targetType, string propertyName) { // If we reach here, no type mismatch - return the value return GetByText(key); } //Place holder until database is build public static string GetByText(string text) { return text; } }
Added the prefix to my web.config
<system.web> <compilation debug="true" defaultLanguage="c#" targetFramework="4.0"> <expressionBuilders> <add expressionPrefix="localizeByText" type ="LocalizationExpressionBuilder"/> </expressionBuilders> </compilation> </system.web>
And I can now get my text like this
<asp:TemplateField HeaderText='<%$ localizeByText:Some text %>'>
解决方案You can build your own custom
Expression Builder
which calls yourGetById
method. Look at the following link for an old but good article explaining how to build an expression builder and how to use it:http://www.4guysfromrolla.com/articles/022509-1.aspx
When you have an expression builder, you use it with the
<%$
syntax. This is different from the databinding syntax<%#
.For the HeaderText field, it is not allowed to use DataBinding syntax (not sure why, but that's how MS made it). Using expression syntax IS allowed and will work once you have your custom expression builder done.
Do go through the page I linked to, it's quite a lot of text, but in the end making you expression builder will not take much effort...
Also, the page has a link at the bottom to a library of expression builder that the author has made. Have a look at them, maybe one of them could be used directly to solve your problem (specifically, the
CodeExpressionBuilder
).这篇关于设置的TemplateField的HeaderText动态本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!