无法加载类型“HomePage.aspx.cs”

3

我正在实现一个小型网站,将从用户那里获取输入并与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文件夹中的内容。 - Rex
2个回答

2

您只需要类名,而非完整文件名,来使用inherits属性。

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

@user3423255 请尝试清理并重新构建解决方案。 - benni_mac_b

1
“继承”属性不应包含文件扩展名。这是Microsoft文档关于“继承”属性的说明:
“继承”: 定义页面要继承的代码后置类。这可以是任何从Page类派生的类。此属性与CodeFile属性一起使用,后者包含代码后置类的源文件路径。当使用C#作为页面语言时,“继承”属性区分大小写;当使用Visual Basic作为页面语言时,“继承”属性不区分大小写。
因此,“CodeBehind”(或Web站点项目的“CodeFile”)属性应该具有文件路径,而“Inherits”属性仅包含类名。尝试将Inherits="HomePage.aspx.cs"替换为Inherits="HomePage",如果适用,包括命名空间。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接