I’m moving from PHP to ASP.NET/C#. So far so good, but I don’t understand how to instantiate a class within a ASP.NET C# class page.
For example I have 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"
}
}
}
In other words, I want to use methods from different classes with the classes I have setup with ASP.NET C#. Namespaces didn’t work for me thus far.
Thank you for any input.
If I undertstood you correctly, wrap
CustomSanitizein a namespace i.e:Then wrap
LoginService.csin the same namespace. i.eYou should now have access to
CustomSanitize a = new CustomSanitize();You can also create more namespaces to split up your project. i.e
namespace MyProject.Helperto wrap all your helper functions, then just addusing MyProject.Helperto the top of the.csfile you wish to use the classes on.Edit:
Please note, when adding a namespace to your project / wrapping classes in a namespace you need to reference them via that namespace via
usingor directly likeMyProject.LoginService. This must be done onaspx,ascxpages as well using the@ Page declaration.