templatefield 动态,设置的TemplateField的HeaderText动态本地化

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

ToolTip=''/>

But I want to change

To

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

And I can now get my text like this

解决方案

You can build your own custom Expression Builder which calls your GetById method. Look at the following link for an old but good article explaining how to build an expression builder and how to use it:

When you have an expression builder, you use it with the

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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值