【问题标题】:ASPNET: Make sure that the class defined in this code file matches the 'inherits' attributeASPNET:确保此代码文件中定义的类与“继承”属性匹配
【发布时间】:2015-04-30 03:56:05
【问题描述】:
namespace ASPMultilingual { 
public partial class _Default : System.Web.UI.Page
{
    ResourceManager rm;
    CultureInfo ci;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Lang"] == null) { 
            Session["Lang"] ="en-US";
        }

        if (!IsPostBack)
        {
            LoadString();

        }

    }

    private void LoadString(){

        Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["Lang"].ToString());
        //rm = new ResourceManager("ASPMultilingual.App_GlobalResources.Lang", Assembly.GetExecutingAssembly());
        ResourceManager rm = new ResourceManager("ASPMultilingual.Lang", System.Reflection.Assembly.Load("ASPMultilingual"));
        ci = Thread.CurrentThread.CurrentCulture;


        btnLogIn.Text = rm.GetString("Login", ci);
    }

    protected void btnLogIn_Click(object sender, EventArgs e)
    {
        string ID = Request.Form["txtID"];
        String password = Request.Form["txtPassword"];
        string strConString = ConfigurationManager.ConnectionStrings["SOConnectionString"].ConnectionString;
        OleDbConnection conn = new OleDbConnection(strConString);
        OleDbCommand cmd = new OleDbCommand("SELECT * FROM USERMASTER", conn);

        try
        {

            conn.Open();
            OleDbDataReader dr;

            dr = cmd.ExecuteReader();
            while (dr.Read()) {
                string testposition = dr["UserPosition"].ToString();
                string dataID = dr["UserId"].ToString();
                string dataPass = dr["UserPwd"].ToString();
                if (dataPass == txtPassword.Text && dataID == txtID.Text)
                {
                    Session["User_Position"] = testposition;
                    Response.Redirect("Default2.aspx");
                }
                else {

                    lblError.Text = "Invalid account! Please Enter again!";
            }

            }


        }
        catch (Exception ex)
        {
            txtID.Text = "ex";
            lblError.Text = ex.ToString();


        }
        finally
        {
            conn.Close();
            conn.Dispose();
        }


        //Response.Redirect("Default2.aspx");
        //ClientScript.RegisterStartupScript(this.GetType(), "yourMessage", "alert('" + ID + " " + password + "');", true);

    }

    protected void ddLang_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["Lang"] = ddLang.SelectedValue;
        LoadString();
    }
}
}

代码运行良好,直到我在代码之上添加命名空间,然后它抛出错误。

编译错误 说明:在编译服务此请求所需的资源期间发生错误。请查看以下具体错误详情并适当修改您的源代码。

编译器错误信息:

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

来源错误: 第 19 行:公共部分类 _Default

【问题讨论】:

  • 如果将命名空间名称更改为其他名称,例如namespace ASPMultilingual2,是否会发生这种情况?
  • 它还是一样...每当我添加命名空间 xxxxx{ 时,我都会收到编译错误...实际上命名空间本身并不是自己的,我在编写所有代码后添加了它。

标签: c# asp.net


【解决方案1】:

您需要在inherits属性的aspx页面中的类名之前添加命名空间。

<%@ Page Title="Some Title" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ASPMultilingual._Default" %>

【讨论】:

    【解决方案2】:

    .aspx 页面中的Page Directive 包含两个重要属性:-

    CodeBehind - 指定与标记 (.aspx) 页面关联的代码隐藏文件。

    Inherits - 现在由于 CodeBehind 属性指定的类后面的代码是 C# 类,并且我们知道在命名空间中我们有多个类,因此使用 Inherits 属性您需要指定确切的与您的标记页关联的完全限定类名。

    所以你的 Page 指令应该是这样的:-

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
                    Inherits="ASPMultilingual._Default" %>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    相关资源
    最近更新 更多