3

私のシナリオは次のとおりです。

GridViewがあり、その中の行を表すクラスを作成しました。セル内のクラスのプロパティと関数の結果を表示することを期待していました。

このために私はこのような列を使用しています:

<asp:TemplateField HeaderText="1">
    <ItemTemplate>
        <asp:Label runat="server" text='<%# MatchesCount((Int32)1) %>' />
    </ItemTemplate>
</asp:TemplateField>

クラスには次の機能があります。

public class MatchesGridViewRow
{
...
    public string MatchesCount(int day) {...}
}

そして私はこのようにGridViewにバインドします:

GridView.DataSource = GetGridViewData(DateTime.Now.Month);
GridViewCalendar.DataBind();    

private List<MatchesGridViewRow> GetGridViewData(int month);

私が得るエラーは次のとおりです:CS0103:名前'MatchesCount'は現在のコンテキストに存在しません。

これはメソッドを呼び出す正しい方法ではありませんか?そうでない場合は、どのように呼び出す必要がありますか?今からありがとう、答えを探しています。

4

2 に答える 2

3

メソッドを静的メソッドに変更するには、これを試してください

public static  string MatchesCount(int day) {...}

その後、電話

'<%# MatchesGridViewRow.MatchesCount((Int32)1)%>'

これと同じ問題を確認してください

gridViewのEval( "")を使用して、.ASPXファイルからパブリック静的クラスファイルにアクセスする

于 2013-03-08T17:43:49.380 に答える
1

私は以下のコードをチェックし、それは役に立ちます:私は願っています

 <asp:TemplateField HeaderText="type" ItemStyle-Width="200">
                    <ItemTemplate>
                        <asp:Label ID="lbtype" runat="server" Text='<%# GetDescrptionHumanType(Convert.ToInt32(Eval("HumanType"))) %>' ></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="200px"></ItemStyle>
                </asp:TemplateField>

   public  static string GetDescrptionHumanType(int val)
    {
       return  Utility.EnumEx.GetEnumDescription((HumanResourceType)val);

    }


   public static string GetEnumDescription(Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes != null &&
            attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }
于 2013-03-09T18:22:09.213 に答える