【问题标题】:Couldn't load type 'HomePage.aspx.cs'无法加载类型“HomePage.aspx.cs”
【发布时间】:2016-03-05 22:10:47
【问题描述】:

我正在实现一个小型网站,该网站将从用户那里获取输入并与 c# 中的数据库进行交互,但问题是后面的代码(.aspx.cs 文件中的代码)不会读取.aspx 文件,虽然我确实将 .aspx 文件指令中的继承属性分配为 .aspx.cs 文件。

这是 HomePage.aspx 文件

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

<!DOCTYPE html PUBLIC "-//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">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="lbl_username" runat="server" Text="Username:   "></asp:Label>
        <asp:TextBox ID="txt_username" runat="server"></asp:TextBox>

        <asp:Label ID="lbl_password" runat="server" Text="Password:   "></asp:Label>
        <asp:TextBox ID="txt_password" runat="server" TextMode="Password"></asp:TextBox>

        <asp:Button ID="btn_login" runat="server" Text="Login" onclick="login" />

    </div>
    </form>
</body>
</html>

这是 HomePage.aspx.cs 文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


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

        }

        protected void login(object sender, EventArgs e)
        {
            string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
            SqlConnection conn = new SqlConnection(connStr);

            SqlCommand cmd = new SqlCommand("loginProcedure", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            string username = txt_username.Text;
            string password = txt_password.Text;
            cmd.Parameters.Add(new SqlParameter("@username", username));

            SqlParameter name = cmd.Parameters.Add("@password", SqlDbType.VarChar, 50);
            name.Value = password;

            // output parm
            SqlParameter count = cmd.Parameters.Add("@count", SqlDbType.Int);
            count.Direction = ParameterDirection.Output;

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            if (count.Value.ToString().Equals("1"))
            {

                Response.Write("Passed");

            }
            else
            {
                Response.Write("Failed");
            }
        }
    }

我收到错误无法加载类型“HomePage.aspx.cs”,那么我该如何处理此类事件?

【问题讨论】:

  • 清理并重建。也删除您的 Obj 文件夹中的内容。

标签: c# asp.net


【解决方案1】:

Inherits 属性不应包含文件扩展名。以下是the Microsoft documentation 对 Inherits 属性的评价:

继承

为要继承的页面定义一个代码隐藏类。这可以是任何 派生自 Page 类的类。此属性与 CodeFile 属性,其中包含源文件的路径 代码隐藏类。 Inherits 属性在使用时区分大小写 C# 作为页面语言,使用 Visual Basic 时不区分大小写 作为页面语言。

所以CodeBehind(或网站项目的CodeFile)属性应该有文件路径,而Inherits 属性只包含类名。尝试将Inherits="HomePage.aspx.cs" 替换为Inherits="HomePage",包括命名空间(如果适用)。

【讨论】:

    【解决方案2】:

    继承属性只需要类名而不是完整文件名

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

    【讨论】:

    • @user3423255 尝试清理并重建解决方案。
    猜你喜欢
    • 1970-01-01
    • 2012-08-14
    • 2015-08-28
    • 2014-08-09
    • 2015-04-16
    • 2010-09-08
    • 2017-04-13
    • 2014-01-03
    • 2013-12-13
    相关资源
    最近更新 更多