无法在ASP.NET中调用全局函数

时间:2014-11-19 03:30:02

标签: c# asp.net

我是ASP.NET的新手,我有一些函数在我的项目中多次出现。我决定将它们夹在我的App_code文件夹中的单个类文件中。

在下面的代码中,我可以调用TxtStrRight和TxtStrLeft,但是当我使用GetDatDiff时,我收到错误"在当前上下文中不存在"。

有人可以解释一下我做错了什么。

我用

调用此方法
<%# GetDiffDate(Convert.ToDateTime(Eval("DateTimeLogged"))) %>

我的App-code文件夹中的GlobalUtilities类文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Generic utilities that can be accessed from any page
/// </summary>
public static class GlobalUtilities
{

    //Takes x characters from the right hand side. Use Looks like MyString.TxtStrRight(8)
    public static string TxtStrRight(this string value, int length)
    {
        if (String.IsNullOrEmpty(value)) return string.Empty;

        return value.Length <= length ? value : value.Substring(value.Length - length);
    }

    //Takes x characters from the left hand side. Use Looks like MyString.TxtStrLeft(40)
    public static string TxtStrLeft(this string value, int length)
    {
        if (String.IsNullOrEmpty(value)) return string.Empty;

        return value.Length <= length ? value : value.Substring(0, length) + "...";
    }

    //Get the difference between time and date of NOW and the database value.
    public static string GetDiffDate(DateTime dt)
    {
        TimeSpan ts = dt - DateTime.Now;
        if (Math.Abs(ts.TotalHours) < 24 && Math.Abs(ts.TotalHours) >= 1)
        {
            return string.Format("{0:0} hrs ago", Math.Abs(ts.TotalHours));
        }
        else if (Math.Abs(ts.TotalHours) < 1)
        {
            return string.Format("{0:0} mins ago", Math.Abs(ts.TotalMinutes));
        }
        else
        {
            return dt.ToString("dd MMM yyyy");
        }
    }

}

1 个答案:

答案 0 :(得分:1)

试试这个(添加类名):

<%# GlobalUtilities.GetDiffDate(Convert.ToDateTime(Eval("DateTimeLogged"))) %>