0

私はデータソースコントロールとリストビューコントロールを持っています

データソースが持っている

"StudentID,StudentName , Birthday ,Gander(M,F),Course_ID"

listViewに表示したい

("Age -> not BirthDay, Gander(male, female)-> not f or' m, and CourseName -> not courseID :))

私はこのようにこの作業を行うためのいくつかのメソッドを書きます

    public  string CalculateAge(DateTime birthDate)
      {
            // cache the current time
            DateTime now = DateTime.Today; // today is fine, don't need the timestamp from now
            // get the difference in years
            int years = now.Year - birthDate.Year;
            // subtract another year if we're before the
            // birth day in the current year
            if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
                 --years;

            return years.ToString(CultureInfo.InvariantCulture);
      }

しかし、どうすれば aspx ファイルでこのメソッドを使用できますEval()ListView? 注: このメソッドは別の名前空間に書きました

4

1 に答える 1

2

でこれを使用しても問題はありませんListView。このようなものが機能するはずです:

<%# CalculateAge((DateTime)Eval("SomeDate")) %>

この関数がを実装するライブラリ内に含まれている場合IDisposableは、コードビハインドでパススルー関数を作成できます。

public string CalculateAge(DateTime birthDate)
{
    using (var obj = new MyObject())
    {
        return obj.CalculateAge(birthDate);
    }
}
于 2012-05-07T15:57:10.033 に答える