加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 为本地化设置TemplateField HeaderText动态

发布时间:2020-12-16 06:25:07 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试为我的ASP.NET代码创建本地化,但是我在设置TemplateField的HeaderText时遇到了问题 我有这个有效 asp:TemplateField HeaderText="Description" ItemTemplate %# Eval("Description") % /ItemTemplate FooterTemplate asp:Panel ID="Panel5" runat=
我正在尝试为我的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") %>'>

但后来我明白了

Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.TemplateField does not have a DataBinding event.

我该如何设置此字段?我可以找到一些使用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).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读