C# 确保此代码文件中定义的类与 'inherits' 属性匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10037856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:13:43  来源:igfitidea点击:

Make sure that the class defined in this code file matches the 'inherits' attribute

c#asp.netsql-serverinheritance

提问by Yaknowss

I am setting up a custom tab where users can view and edit data. I need to establish a trusted SQL connection and have the data displayed in a grid view.

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

Should I build a console or web app?

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

I have provided my .aspx and aspx.cs files below.

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

I get the error message below while running it:

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

"Error: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl)".

“错误:确保此代码文件中定义的类与 'inherits' 属性匹配,并且它扩展了正确的基类(例如 Page 或 UserControl)”。

Here is my Default.aspx code:

这是我的 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>

Here is my Default.aspx.cs code:

这是我的 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();
            }
        }
    }
}

采纳答案by Tieson T.

Your code-behind page must inherit from System.Web.UI.Page, like so:

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

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

}

回答by Krishna

If you want to display data in a gridview, then console application doesn't make sense.. its gotta be a web application. Coming to your error.. post the top line in your aspx file here it looks something like this

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

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

回答by Francisco Garcia

Although there is already a selected answer, and my answer doesn't exactly apply to this particular problem, I've encountered this more than once and there are occasionally alternate causes. Just this morning I had this error when I attempted a build and I was correctly inheriting from System.Web.UI.Page. In fact I scoured the code files and everything looked totally fine. Upon close inspection, though, I discovered what was missing was a single closing bracket on a conditional statement that somehow caused the unrelated error.

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

I'm not saying my answer is the correct answer for this problem as the count of open and closing brackets seems fine in the question's code, I'm just saying it's worth checking. If this saves a person 10 minutes, it was worth my time. Thanks!

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

回答by Emi

I had this problem as I was using visual studio 2005, I just removed the namespace section of the code and that cleared all the errors

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

I made it like so:

我是这样制作的:

public partial class bla : System.Web.UI.Page { all your codes and stuff}

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

And that was how it worked for me.

这就是它对我有用的方式。