asp.net-全局使用的公共静态函数

asp.net-全局使用的公共静态函数,asp.net,Asp.net,我是ASP.NET C#的新手。我试图按照本教程页面制作一个全局使用的函数,但运气不好。 我尝试在代码中的任何地方使用全局函数。我有一个名为“FormatDateNoTime”的函数。我已经在App_Code文件夹下创建了一个类文件。但当我在一个代码隐藏页(例如Page1.aspx.cs)中调用该函数时,它会给出错误: 错误:名称“MyClass”在当前上下文中不存在 App_Code文件夹下的MyClass.cs文件 using System; using System.Collection

我是ASP.NET C#的新手。我试图按照本教程页面制作一个全局使用的函数,但运气不好。

我尝试在代码中的任何地方使用全局函数。我有一个名为“FormatDateNoTime”的函数。我已经在App_Code文件夹下创建了一个类文件。但当我在一个代码隐藏页(例如Page1.aspx.cs)中调用该函数时,它会给出错误:

错误:名称“MyClass”在当前上下文中不存在

App_Code文件夹下的MyClass.cs文件

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

/// <summary>
/// Summary description for Class1
/// </summary>
public class MyClass
{

        //
        // TODO: Add constructor logic here
        //

        public static string FormatDateNoTime(string input)            {
            string thedate;
            DateTime strDate = DateTime.Parse(input);
            thedate = strDate.ToString("MM/dd/yyyy");
            return thedate;
        }
}
其他页面似乎无法识别此类。function()


请帮忙。提前感谢。

右键单击应用程序代码中的源文件,并将其的“构建操作”属性设置为“编译”。单击App_Code中的.cs文件并点击F4键(或右键单击->属性),您将看到的“构建操作”选项
这样,它将在你的App_代码文件夹中构建代码,你应该能够在你的类中访问你的静态方法。如果上述方法无效,请从应用程序代码中删除该类,并将其放入根文件夹中,然后尝试编译。

您拥有的代码无法编译。我敢肯定,当您尝试运行VisualStudio时,它会抛出一个错误;注意它在说什么

此外,请更改以下内容:

public static string FormatDateNoTime(object sender, EventArgs e)
{
    string thedate;
    DateTime strDate = DateTime.Parse(input);
    thedate = strDate.ToString("MM/dd/yyyy");
    return thedate;
}


我将进一步验证输入是否可以解析为DateTime,但这需要您去探索。

我看到您在上面编写的代码段中出现了几个编译错误:一个是,确保您将MyClass类中的输入定义为静态变量,第二个是,在调用FormatDateNotTime方法时,您只传递了一个参数。再看一看,代码永远不会被编译:调用函数时参数数量错误,函数会查找不存在的输入变量。这是cs101的东西,我不太明白。如果可能的话,你能帮我更正上面的代码吗?谢谢回复。为什么要传递
对象
事件参数
参数??它们是为事件处理程序准备的..对不起,我是新来的。我已经更新了代码以传递字符串。仍然无法从另一页识别MyClass.FormatDateNotTime函数。感谢您的重播。我右键单击MyClass.cs文件并点击F4,但没有看到任何“构建操作”,我只看到两个文件名和完整页面。有什么建议吗?右键单击MyClass.cs文件并转到属性。它应该是这样的:谢谢@tranceporter,我认为这是一条正确的路径,但是我无法理解如何在App_代码中从“Build Action”到“Compile”的cs文件,谢谢您的回复。我按照您的建议更改了代码,但仍然收到错误“错误:名称‘MyClass’在当前上下文中不存在”。所以我需要编译这个类?怎么用?对不起,我是新来的。
public static string FormatDateNoTime(object sender, EventArgs e)
{
    string thedate;
    DateTime strDate = DateTime.Parse(input);
    thedate = strDate.ToString("MM/dd/yyyy");
    return thedate;
}
public static string FormatDateNoTime(String input)
{
    string thedate;
    DateTime strDate = DateTime.Parse(input);
    thedate = strDate.ToString("MM/dd/yyyy");
    return thedate;
}