2015-12-01 46 views
3

我正在實現一個小型網站,將從用戶接受輸入並與c#中的數據庫進行交互,但問題在於後面的代碼(.aspx中的代碼.cs文件)不會讀取.aspx文件中的任何元素,但我確實將.aspx文件的指令中的inherit屬性指定爲.aspx.cs文件。無法加載類型'HomePage.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 ',那麼我該如何處理這樣的事件呢?

+0

清潔和重建。也刪除你的Obj文件夾中的東西。 – Rex

回答

2

你只需要在繼承屬性

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

我改變了它,但仍然有相同的錯誤?! – user3423255

+0

@ user3423255嘗試清理並重建解決方案。 –

1

Inherits屬性不應該包含文件擴展名的類名不完整的文件名。下面是the Microsoft documentation說關於繼承財產:

繼承

定義代碼隱藏類的頁面繼承。這可以是派生自Page類的任何 類。此屬性與 CodeFile屬性一起使用,該屬性包含 代碼隱藏類的源文件的路徑。當使用 C#作爲頁面語言時,Inherits屬性區分大小寫,使用Visual Basic 作爲頁面語言時不區分大小寫。

因此,CodeBehind(或CodeFile用於Web站點項目)屬性應具有文件路徑,而Inherits屬性僅包含類名稱。嘗試用Inherits="HomePage"替換Inherits="HomePage.aspx.cs",包括命名空間(如果適用)。

+0

我改變了它,但仍然有相同的錯誤?! – user3423255

相關問題