Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET页使用的外部类_C#_Asp.net_Class - Fatal编程技术网

C# ASP.NET页使用的外部类

C# ASP.NET页使用的外部类,c#,asp.net,class,C#,Asp.net,Class,我是一个在ASP.NET中使用C#应用程序的新手,所以我请你耐心等待 首先是上下文:我开发了一个ASP页面,用于验证用户名和密码(如第一段代码所示。对于这个问题的效果,密码框中的字符无关紧要,这与此无关) Index.aspx <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body>

我是一个在ASP.NET中使用C#应用程序的新手,所以我请你耐心等待

首先是上下文:我开发了一个ASP页面,用于验证用户名和密码(如第一段代码所示。对于这个问题的效果,密码框中的字符无关紧要,这与此无关)

Index.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="Login" runat="server">
    <div><table>
    <tr>
    <td>User</td>
    <td><asp:TextBox ID="User" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><asp:TextBox ID="Pass" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td></td>
    <td><asp:Button ID="LoginButton" runat="server" Text="Login" 
        onclick="LoginButton_Click" /></td>
    </tr></table>
    </div>
    </form>
    </body>
    </html>
让我们假设在某个时刻,我需要创建一个新类,专门用于验证登录(我知道可以用Java完成),我将在我的页面中使用它。在这种情况下,是否有必要考虑我已经在使用
Index.aspx.WebDesigner.cs
?如果有必要,或者我别无选择,只能使用这个新类,我该如何创建它呢?

用c#创建类与用任何现代的强类型OO编程语言创建类非常相似。首先定义类,然后实例化它。在您的问题中,有许多不同的方法可以重新创建验证,这里有一种

下面是类的定义

public class Validator
{
  private const string Username = "Carlos";
  private const string Password = "236";

  public bool Validate(string user, string pass) 
  {
    return (user == Username && pass == Password);
  }
}
在代码中实例化和使用类(注意使用而不是if/else,这样可以保持代码简洁易读)

C#是一门深奥的语言,学习曲线柔和,你可以从找到一本关于这门学科的好教程或书中获益

另一件需要注意的事情是,这个词在c#中是一个关键字,表示托管代码(即在中运行的代码)希望加载和执行非托管代码(即本机运行的代码)。

我说的是“extern”类,在您所说的上下文中不是,但在该类完全独立于ASP页的上下文中是这样的。但无论如何,谢谢你的夸奖。
public class Validator
{
  private const string Username = "Carlos";
  private const string Password = "236";

  public bool Validate(string user, string pass) 
  {
    return (user == Username && pass == Password);
  }
}
protected void LoginBoton_Click(object sender, EventArgs e)
{
  //instantiate the class defined above
  var validator = new Validator();

  //find the next page to redirect to
  var redirectTo = validator.Validate(User.Text, Pass.Text) ? "Valid.aspx" : "Invalid.aspx";

  //redirect the user
  Response.Redirect(redirectTo);
}