简体   繁体   English

为什么我不能在 aspx.cs 文件中使用我的 c# 类?

[英]Why cant I use my c# classes in aspx.cs files?

My c# class code (Class1.cs):我的 c# 类代码 (Class1.cs):

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

namespace Montajat.App_Code
{
    public class Class1
    {

    }
}

default.aspx.cs code: default.aspx.cs 代码:

public partial class _default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

How can I use Class1 in Page_Load of my aspx.cs file and why can`t I use it at the moment ?如何在我的 aspx.cs 文件的 Page_Load 中使用 Class1,为什么我现在不能使用它?

on top include namespace :在顶部包含命名空间

using Montajat.App_Code;

public partial class _default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     Class1 objClass1 = new Class1();
    }
}

or you can do it using fully qualified name:或者您可以使用完全限定名称来执行此操作:

public partial class _default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     Montajat.App_Code.Class1 objClass1 = new Montajat.App_Code.Class1();
    }
}

your class:你的班:

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

namespace Montajat.App_Code
{
    public class Class1
    {
      public Class1()
      {

      }
    }
}
public partial class _default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     var objClass1 = new Montajat.App_Code.Class1();
    }
}

Does this works?这行得通吗? Maybe you are missing the namespace?也许您缺少命名空间?

Also, verify that your website has a reference to the project where you class is located.此外,请验证您的网站是否引用了您班级所在的项目。 If you are not in the same project, you need to have a dll reference to the one that contain your class.如果你不在同一个项目中,你需要有一个包含你的类的 dll 引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM