Unable to call App_Code class from a code-behind

≡放荡痞女 提交于 2019-11-26 11:26:07

问题


I have a class in a file that\'s in the \"App_Code\" folder. I\'m able to use this in an \"aspx\" file but not from a code-behind file. How do I make it visible to a code-behind?

NOTE: This is ASP.Net on Mono and I\'m writing the classes directly, not using an IDE to compile them

My files:

ASPX FILE (testappcode.aspx)

<%@ Page language=\"c#\" src=\"TestAppCode.aspx.cs\" Inherits=\"TestAppCode.TestAppCode\" AutoEventWireup=\"true\" %>
<html>
  <head>
    <title>Test App_Code Folder</title>
  </head>
  <body>
    <form id=\"contactForm\" runat=\"server\">
    <asp:TextBox id=\"Name\" runat=\"server\" ></asp:TextBox>
    <asp:TextBox id=\"Age\" runat=\"server\" ></asp:TextBox>
    <asp:Button ID=\"Submit\" runat=\"server\" Text=\"Submit\" onclick=\"SubmitForm\" />
    </form>
  </body>
<html>

CODE BEHIND (TestAppCode.aspx.cs)

using System;
using System.Web.UI.WebControls;

namespace TestAppCode
{
    public class TestAppCode : System.Web.UI.Page
    {
    protected void SubmitForm(object sender, EventArgs e)
    {
        //It fails here with the error: CS0246: The type or namespace name
        //`MyAppCodeClass\' could not be found. Are you missing a using
        //directive or an assembly reference?
        MyAppCodeClass m = new MyAppCodeClass();
    }
    }
}

APP_CODE CLASS (App_Code/MyAppCodeClass.cs)

public class MyAppCodeClass
{
         public MyAppCodeClass() {}
}

I tried giving it a namespace, but that doesn\'t fix the issue.


回答1:


Change your class' Build Action to Compile.



来源:https://stackoverflow.com/questions/14204990/unable-to-call-app-code-class-from-a-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!