【问题标题】:Make sure that the class defined in this code file matches the 'inherits' attribute确保此代码文件中定义的类与“继承”属性匹配
【发布时间】:2012-04-06 00:35:41
【问题描述】:

我正在设置一个自定义选项卡,用户可以在其中查看和编辑数据。我需要建立一个受信任的 SQL 连接并将数据显示在网格视图中。

我应该构建控制台还是 Web 应用程序?

我在下面提供了我的 .aspx 和 aspx.cs 文件。

我在运行时收到以下错误消息:

"错误:确保此代码文件中定义的类与 'inherits' 属性,并且它扩展了正确的基类(例如 页面或用户控件)”。

这是我的 Default.aspx 代码:

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <headrunat="server">
        <title></title>
    </head>
    <body>
        <formid="form1"runat="server">
        </form>
    </body>
</html>

这是我的 Default.aspx.cs 代码:

///<summary>
///Demonstrates how to work with SqlConnection objects
///</summary>
class SqlConnectionDemo
{
    static void Main()
    {
        // 1. Instantiate the connection
        SqlConnectionconn = newSqlConnection("Data Source=TestDB;Initial Catalog=Impresario;Integrated Security=SSPI");
        SqlDataReaderrdr = null;
        try
        {
            // 2. Open the connection
            conn.Open();
            // 3. Pass the connection to a command object
            SqlCommandcmd = newSqlCommand("select * from LT_WEB_DONATIONS_EXTRA_INFO_TEMP", conn);

            //
            // 4. Use the connection
            //
            // get query results
            rdr = cmd.ExecuteReader();

            // print the CustomerID of each record
            while (rdr.Read())
            {
            Console.WriteLine(rdr[0]);
            }
        }
        finally
        {
            // close the reader
            if(rdr != null)
            {
                rdr.Close();
            }

            // 5. Close the connection
            if(conn != null)
            {
                conn.Close();
            }
        }
    }
}

【问题讨论】:

  • 让它更具可读性,也许人们会尝试帮助你..

标签: c# asp.net sql-server inheritance


【解决方案1】:

虽然已经有一个选定的答案,而且我的答案并不完全适用于这个特定问题,但我不止一次遇到过这种情况,并且偶尔会有其他原因。就在今天早上,当我尝试构建并且我正确地从 System.Web.UI.Page 继承时,我遇到了这个错误。事实上,我搜索了代码文件,一切看起来都很好。但是,经过仔细检查,我发现缺少的是条件语句上的一个右括号,它以某种方式导致了不相关的错误。

我并不是说我的答案是这个问题的正确答案,因为在问题的代码中开括号和右括号的计数似乎很好,我只是说值得检查。如果这可以节省一个人 10 分钟,那值得我花时间。谢谢!

【讨论】:

  • 阅读您的评论后,我发现该错误不是由 ASP.NET 所说的内容引起的。它告诉我母版页有该错误,但该错误实际上是由用户控件中的错误引起的。
【解决方案2】:

您的代码隐藏页面必须继承自 System.Web.UI.Page,如下所示:

public partial class _Default : System.Web.UI.Page
{

}

【讨论】:

    【解决方案3】:

    如果您想在网格视图中显示数据,那么控制台应用程序没有意义。它必须是 Web 应用程序。 来到你的错误..在你的 aspx 文件中发布第一行,它看起来像这样

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs"
        Inherits="Login" MasterPageFile="~/MasterPage.master" %>
    

    【讨论】:

      【解决方案4】:

      我在使用 Visual Studio 2005 时遇到了这个问题,我刚刚删除了代码的命名空间部分并清除了所有错误

      我是这样设计的:

      公共部分类 bla : System.Web.UI.Page { 你所有的代码和东西 }

      这就是它对我的作用。

      【讨论】:

      • 欢迎来到 Stack Overflow!花点时间阅读帮助中心的editing help。 Stack Overflow 上的格式与其他网站不同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      相关资源
      最近更新 更多