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イベントを持つオブジェクトでのみサポートされます。System.Web.UI.WebControls.TemplateFieldにはDataBindingイベントがありません。
このフィールドはどのように設定すればよいですか?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 %>'>