簡體   English   中英

從ASP.NET C#類頁面實例化C#類

[英]Instantiating C# class from ASP.NET C# class page

我正在從PHP遷移到ASP.NET / C#。 到目前為止一直很好,但我不明白如何在ASP.NET C#類頁面中實例化一個類。

例如,我有login.aspx:

<%@ Page Language="C#" Inherits="Portal.LoginService" src="LoginService.cs" %>

<!DOCTYPE html>
<body>
    <form runat="server" id="login" name="login">           
        <p>Username:</p><input type="text" id="username" name="username" runat="server" />  

        <p>Password:</p><input type="password" id="password" name="password" runat="server" />

        <input type="submit" name="submit" OnServerClick="Post_Form" runat="Server" /> 
    </form>
</body>

LoginService.cs:

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
namespace Portal {
public class LoginService : Page {

    protected HtmlInputControl username, password;

    public void Post_Form(object sender, EventArgs e) {

        if (username.Value != "" && password.Value != "") {
            //LOGIC HERE

            //This doesn't work
            CustomSanitize a = new CustomSanitize();                              
        }
    }
}
}

CustomSanitize.cs:

using System;
namespace Portal {
    public class CustomSanitize {

        //instantiate here

        public string sanitizeUserPass() {
                return "logic here"
            }
    }
}

換句話說,我想使用不同類中的方法和我使用ASP.NET C#設置的類。 到目前為止,命名空間對我來說並不起作用。

謝謝你的任何意見。

如果我正確地執行了您, CustomSanitize包裝在命名空間中,即:

namespace MyProject {
    public class CustomSanitize {

        //instantiate here

        public string sanitizeUserPass() {
                return "logic here"
            }
    }
}

然后將LoginService.cs包裝在同一名稱空間中。

namespace MyProject {
    public class LoginService : Page {

        protected HtmlInputControl username, password;

        public void Post_Form(object sender, EventArgs e) {

            if (username.Value != "" && password.Value != "") {
                //LOGIC HERE

                //This doesn't work
                //CustomSanitize a = new CustomSanitize();                              
            }
        }
    }
}

您現在應該可以訪問CustomSanitize a = new CustomSanitize();

您還可以創建更多名稱空間來拆分項目。 namespace MyProject.Helper來包裝所有幫助函數,然后using MyProject.Helper添加到您希望使用類的.cs文件的頂部。

編輯:

請注意,在命名空間中向項目/包裝類添加命名空間時,需要通過using或直接像MyProject.LoginService通過該命名空間引用它們。 這必須使用@ Page declarationaspxascx頁面上完成。

如果您的所有上述類都在同一個項目中,那么您需要做的就是添加命名空間。 請查看以下示例: 命名空間教程

如果您希望在同一個項目中使用不同的命名空間,請在頂部添加您自己的“using”行,即

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Utilities.Sanitizers;

namespace Services {

public class LoginService : Page {

protected HtmlInputControl username, password;

public void Post_Form(object sender, EventArgs e) {

    if (username.Value != "" && password.Value != "") {
        //LOGIC HERE

        //This doesn't work
        //CustomSanitize a = new CustomSanitize();                              
    }
}
}
}

using System;
namespace Utilities.Sanitizers
{
public class CustomSanitize {

    //instantiate here

    public string sanitizeUserPass() {
            return "logic here"
        }
}
}

CustomSanitizeloginservice類中,您缺少項目的命名空間。

除此之外,您可能希望為您的類創建構造函數。 這不是必需的,但允許您在另一個文件中實例化類時設置默認設置。 (當你沒有自己設置時,編譯器會生成一個空構造函數。所以這個代碼示例中的那個是冗余的。但只是為了向你展示。)

這與創建重載的構造函數一樣,允許您將各種參數傳遞給構造函數,以便編譯器選擇正確的參數。

雖然您面臨的主要問題可能是您的項目中沒有包含名稱空間。 當文件在同一個項目中時,您應該為該項目中的所有類添加命名空間。 因此可以在彼此之間引用它們。

如:

using System;

namespace yourNameSpace
{
    public class CustomSanitize 
    {

        public CustomSanitize()
        {
            //Set what you need to set in the constructor
        }
        //instantiate here

        public string sanitizeUserPass() {
            return "logic here"
        }
    }
}

默認情況下,從visual studio中生成新類時,應添加名稱空間。 除非您每次都以空代碼文件開頭。

沒有人提到的一件事是你不需要做這樣的事情來進行用戶身份驗證。 您應該考慮使用開箱即用的.net身份驗證和授權 - 請參閱此處: https//www.google.co.uk/search?q = .NET +authentication &aq = 0&oq = .NET + authen&aqs = chrome。 1.57j0l3j62l2.2451&sugexp =鉻,MOD = 19&的SourceID =鉻&即= UTF-8

然而,其他人都說的是正確的。 這是行不通的,因為你錯過了一個使用的聲明(Intelisense應該告訴你 - 你應該能夠點擊小的下划線位,然后點擊添加使用鏈接)

你做的另一件事是添加如下: -

protected HtmlInputControl username, password;

這在asp.net表單頁面中不是必需的。 我假設你已經做了很多手工工作 - 感謝網絡比這簡單得多。

這更像是頁面的外觀: -

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" AssociatedControlID="Username">Username:</asp:Label>     <asp:TextBox runat="server" ID="Username"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Username" Display="Dynamic" Text="Username is required"></asp:RequiredFieldValidator>
        <br/>
        <asp:Label ID="Label1" runat="server" AssociatedControlID="Password">Password:</asp:Label> <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
    <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" Display="Dynamic" Text="Password is required"></asp:RequiredFieldValidator>
        <br/>
        <asp:Button runat="server" Text="Login" ID="btnLogin" OnClick="btnLogin_Click" />
    </div>
    </form>
</body>
</html>

而背后的代碼:使用System;

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

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                string username = Username.Text;
                string password = Password.Text;
                //Logic here
            }
        }

    }
}

這將為您完成所有驗證。 我真的建議你在這里觀看一些視頻來幫助我們加快速度: http//www.asp.net/web-forms ,但你也應該認真考慮使用MVC,因為它更符合你的嘗試方式發展

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM