4

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 %>'>
4

1 に答える 1

5

Expression Builderメソッドを呼び出す独自のカスタムを作成できますGetById。式ビルダーの作成方法とその使用方法を説明する、古くて優れた記事については、次のリンクを参照してください。

http://www.4guysfromrolla.com/articles/022509-1.aspx

式ビルダーがある場合は、<%$構文とともに使用します。これは、データバインディング構文とは異なります<%#

HeaderTextフィールドの場合、DataBinding構文を使用することは許可されていません(理由はわかりませんが、MSがそれを作成した方法です)。式構文の使用は許可されており、カスタム式ビルダーを実行すると機能します。

私がリンクしたページを読んでください、それはかなりたくさんのテキストです、しかし結局あなたを式ビルダーにすることは多くの努力を要しません...

また、ページの下部には、作成者が作成した式ビルダーのライブラリへのリンクがあります。それらを見てください。おそらく、そのうちの1つを直接使用して問題を解決することができます(具体的にはCodeExpressionBuilder)。

于 2012-07-09T11:36:39.750 に答える