我正在实现一个小网站,它将接受用户的输入并与c#中的数据库交互,但问题是背后的代码( .aspx.cs文件中的代码)不读取.aspx文件中的任何元素,尽管我在.aspx文件的指令中分配了inherit属性为.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‘,那么我该如何处理这样的事件?
发布于 2015-12-02 04:43:29
inherits属性只需要类名,而不需要完整的文件名
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HomePage.aspx.cs" Inherits="HomePage" %>发布于 2015-12-02 04:44:31
Inherits属性不应包含文件扩展名。下面是the Microsoft documentation对Inherits属性的描述:
继承
定义要继承的页的代码隐藏类。这可以是从Page类派生的任何类。此属性与CodeFile属性一起使用,该属性包含代码隐藏类的源文件的路径。Inherits属性在使用C#作为页面语言时区分大小写,在使用Visual Basic作为页面语言时不区分大小写。
因此,CodeBehind (或网站项目的CodeFile )属性应该包含文件路径,而Inherits属性只包含类名。尝试用Inherits="HomePage"替换Inherits="HomePage.aspx.cs",如果适用,包括命名空间。
https://stackoverflow.com/questions/34029694
复制相似问题