2012-05-07 49 views
0

我有數據源控制和ListView控制我怎樣才能在aspx文件使用eval()作爲參數用的ListView

數據源具有

「StudentID,StudentName,生日,甘德(M,F) ,COURSE_ID 「

我想在我的ListView

(顯示」 年齡 - >不是生日,甘德(男,女) - >不是F或」 M和 CourseName - >不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文件中​​在我的ListView?注意:我在不同的命名空間中編寫了此方法

回答

2

ListView中使用此方法不會有任何問題。像這樣的東西應該工作:

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

如果此功能包含實現IDisposable庫中,可以創建在代碼後面的穿通功能:

public string CalculateAge(DateTime birthDate) 
{ 
    using (var obj = new MyObject()) 
    { 
     return obj.CalculateAge(birthDate); 
    } 
} 
+0

感謝詹姆斯有關,但什麼如果我的方法在不同的命名空間 – tito11

+0

那個命名空間是否可以訪問頁面?你可以從代碼隱藏中訪問它嗎? –

+0

是的,我可以訪問它從代碼behing ,,我現在嘗試它 – tito11