asp.net – 为本地化设置TemplateField HeaderText动态
我正在尝试为我的ASP.NET代码创建本地化,但是我在设置TemplateField的HeaderText时遇到了问题
我有这个有效 <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> 但我想改变 <asp:TemplateField HeaderText="Description"> 至 <asp:TemplateField HeaderText='<%# GetById("atforbedringsforslag_description_title") %>'> 但后来我明白了
我该如何设置此字段?我可以找到一些使用OnRowCreated,但随后你访问带有索引号的字段,然后很容易出错或忘记更改索引如果以后添加新字段 编辑我的解决方案: 创建自定义表达式构建器 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; } } 在我的web.config中添加了前缀 <system.web> <compilation debug="true" defaultLanguage="c#" targetFramework="4.0"> <expressionBuilders> <add expressionPrefix="localizeByText" type ="LocalizationExpressionBuilder"/> </expressionBuilders> </compilation> </system.web> 我现在可以得到这样的文字了 <asp:TemplateField HeaderText='<%$localizeByText:Some text %>'> 解决方法
您可以构建自己的自定义表达式生成器来调用GetById方法.请查看以下链接以获取解释如何构建表达式构建器以及如何使用它的旧文章:
http://www.4guysfromrolla.com/articles/022509-1.aspx 如果有表达式构建器,则将其与<%$语法一起使用.这与数据绑定语法<%#不同. 对于HeaderText字段,不允许使用DataBinding语法(不确定原因,但MS就是这样做的).使用表达式语法IS,一旦完成自定义表达式构建器,它就会起作用. 请浏览我链接的页面,它是相当多的文本,但最终让你的表达式构建器不会花费太多精力…… 此外,该页面底部还有一个链接到作者所创建的表达式构建器库.看看它们,也许其中一个可以直接用来解决你的问题(特别是CodeExpressionBuilder). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-2 – 将参数传递给MVC Ajax.ActionLink
- asp.net-mvc – 为什么Add View List Scaffolding向导不使用
- ASP.NET剃刀参考文档
- asp.net – 堆栈溢出原因
- asp.net-mvc – 使用mvc-mini-profiler
- asp.net-core – VS2017:目标框架下拉列表未显示.NET Core
- asp.net-mvc – 如何关闭Visual Studio MVC视图中的大括号和
- asp.net – SignalR(Hub)可以发送除信号制作者之外的消息吗
- asp.net-mvc – asp.net MVC:本地化
- asp.net – NuGet不断崩溃Visual Studio 2013
- asp.net-mvc-3 – Value Injecter是否会映射集合
- 采用Opserver来监控你的ASP.NET项目系列(二、监控
- dependency-injection – asp.net 5依赖注入拦截
- asp.net – 内部图像和文本的asp:LinkBut??ton在
- asp.net – 如何使用web-api上传文件
- ASP.NET MVC 2.0 – RenderPartial和RenderActio
- 将ASP.NET的“ReturnURL”转换为绝对URL
- asp.net – 自定义Web字体在IE9中不起作用
- asp.net验证文本框 – 至少有一个文本框必须包含
- asp.net – 为什么当StateProvider不是InProc时,