我如何使用 Eval() 作为 aspx 文件中的参数与列表视图

本文关键字:参数 视图 列表 文件 aspx 何使用 Eval 作为 | 更新日期: 2023-09-27 18:33:29

>i 有数据源控件和列表视图控件

数据源具有

"学生证,学生姓名,生日,甘德(男,女),Course_ID"

我想显示在我的列表中查看

("年龄->不是出生日,甘德(男性,女性)>不是f或m,和 课程名称 -> 不是课程 ID :))

我写了一些方法来做这项工作,就像这样

    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中使用Eval()?注意:我在不同的命名空间中编写了此方法

我如何使用 Eval() 作为 aspx 文件中的参数与列表视图

ListView中使用它应该没有问题。这样的事情应该有效:

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

如果此函数包含在实现 IDisposable 的库中,则可以在代码隐藏中创建传递函数:

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