c# - Extern class used by an ASP.NET page -


i'm newbie in using c# applied asp.net, ask of things patience.

first context: developed asp page takes validate username , password (as shown in first chunk of code. effects of question, doesn't matter characters in password box, it's irrelevant).

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> 

then after clicking button "login", strings given in both textboxes being compared specific strings, , if both strings coincide, login successful (as shown in second chunk of code).

index.aspx.webdesigner.cs

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols;  namespace webapplication7 {     public partial class index : system.web.ui.page     {         protected void page_load(object sender, eventargs e)         {          }          protected void loginboton_click(object sender, eventargs e)         {             string user = user.text;             string password = pass.text;               string uservalid = "carlos";             string passvalid = "236";              if((user.equals(uservalid)) && (password.equals(passvalid)))                 response.redirect("valid.aspx");             else                 response.redirect("invalid.aspx");          }     } } 

let's suppose in moment i need create new class specifically validating login (i know can done java), , use page. necessary considering in case using index.aspx.webdesigner.cs? , if necessary, or don't have no choice use new class, how can create it?

creating classes in c# similar creating classes in modern, typed, oo programming language. first define class, , instantiate it. there many different ways re-create validation in question, here's one.

here class definition

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);   } } 

to instantiate , use class in code (note use of ternary conditional operator instead of if/else, keeps code concise , readable)

protected void loginboton_click(object sender, eventargs e) {   //instantiate class defined above   var validator = new validator();    //find next page redirect   var redirectto = validator.validate(user.text, pass.text) ? "valid.aspx" : "invalid.aspx";    //redirect user   response.redirect(redirectto); } 

c# deep language gentle learning curve, may benefit finding tutorial or book on subject. there number of introductory tutorials microsoft may helpful.

another thing note, word extern keyword in c#, indicates managed code (i.e. code runs in clr) wants load , execute unmanaged code, (i.e. code runs natively).


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -