C# 如何在带有ListView的aspx文件中使用Eval()作为参数
我有datasource控件和listView控件 数据源已被删除 “学生ID、学生姓名、生日、性别(男、女)、课程ID” 我想在我的列表视图中显示 (“年龄->非生日,雌雄同体->非f或m,以及 CourseName->非courseID:) 我写了一些这样做的方法C# 如何在带有ListView的aspx文件中使用Eval()作为参数,c#,asp.net,listview,C#,Asp.net,Listview,我有datasource控件和listView控件 数据源已被删除 “学生ID、学生姓名、生日、性别(男、女)、课程ID” 我想在我的列表视图中显示 (“年龄->非生日,雌雄同体->非f或m,以及 CourseName->非courseID:) 我写了一些这样做的方法 public string CalculateAge(DateTime birthDate) { // cache the current time DateT
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);
}
公共字符串CalculateAge(日期时间出生日期)
{
//缓存当前时间
DateTime now=DateTime.Today;//今天就可以了,从现在开始不需要时间戳
//以年为单位计算差异
int years=now.Year-生日.Year;
//如果我们在最后期限之前,再减去一年
//本年出生日
if(now.Month
但是我如何在我的aspx文件中使用此方法,并在我的
列表视图中使用Eval()
?注意:我在不同的命名空间中编写了此方法
在列表视图中使用此方法应该不会有问题。类似的方法应该可以工作:
<%# CalculateAge((DateTime)Eval("SomeDate")) %>
谢谢james,但是如果我在不同名称空间中的方法是该页面可以访问的名称空间呢?您可以从代码隐藏中访问它吗?我想您可以指定完整的限定名,即MyApp.Foo.MyFunction()
谢谢但不起作用>>编译器错误消息:CS0103:当前上下文中不存在名称“CalculateAge”,这表明该函数无法访问该页。是否在代码中导入了命名空间?
public string CalculateAge(DateTime birthDate)
{
using (var obj = new MyObject())
{
return obj.CalculateAge(birthDate);
}
}