Use C # for ASP. NET login page

Source: Internet
Author: User
Use C # for ASP. NET login page developer online Builder.com.cn Update Time: 2007-09-08Author: Chinese IT lab Source: Chinese IT lab keywords: Login Page ASP. net c #

1. Create a database
Create an access data user. mdb.
Create a user table and add the UserId and Password fields.
2. Create a new default. aspx file.
In Web Form:
Add two Label controls. The Text attributes are "Logon Name" and "password ";
Add two TextBox controls. The ID attributes are "Userid" and "Pwd", respectively. The Text attributes are empty;
Add two RequiredFieldValidato controls. The ID attributes are "rfvUserid" and "rfvPwd", and the Text attributes are "Enter login name !" And "Enter the logon Password !", The ControlToValidate attributes are "Userid" and "Pwd" respectively ";
Add a Button control. The ID attribute is "LogButton" and the Text gender is "login ";
Add a Label control. The ID attribute is "Msg ".
The source code of Default. aspx is as follows:
<% @ Page language = "c #" Codebehind = "default. aspx. cs" AutoEventWireup = "false" Inherits = "lsj. WebForm1" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Meta name = "GENERATOR" Content = "Microsoft Visual Studio 7.0">
<Meta name = "CODE_LANGUAGE" Content = "C #">
<Meta name = "vs_defaultClientScript" content = "JavaScript (ECMAScript)">
<Meta name = "vs_targetSchema" content = "http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<FONT face = "">
<Form runat = "server" ID = "Form1">
<Asp: Label id = "Label1" style = "Z-INDEX: 101; LEFT: 82px; POSITION: absolute; TOP: 39px" runat = "server" Width = "55px"

Height = "26px"> login name </asp: Label>
<Asp: Label id = "Label2" style = "Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 84px "runat =" server "Width =" 63px "Height =" 24px "> password </asp: Label>
<Asp: TextBox id = "Userid" style = "Z-INDEX: 103; LEFT: 161px; POSITION: absolute; TOP: 39px "runat =" server "Width =" 109px "Height =" 25px "> </asp: TextBox>
<Asp: TextBox id = "Pwd" style = "Z-INDEX: 104; LEFT: 162px; POSITION: absolute; TOP: 81px "runat =" server "Width =" 109px "Height =" 22px "TextMode =" Password "> </asp: TextBox>
<Asp: Button id = "LogButton" style = "Z-INDEX: 105; LEFT: 79px; POSITION: absolute; TOP: 125px "runat =" server "Width =" 59px "Height =" 25px "Text =" login "> </asp: Button>
<Asp: Label id = "Msg" style = "Z-INDEX: 106; LEFT: 161px; POSITION: absolute; TOP: 130px "runat =" server "Width =" 117px "Height =" 26px "> </asp: Label>
<Asp: RequiredFieldValidator id = "RequiredFieldValidator1" style = "Z-INDEX: 107; LEFT: 290px; POSITION: absolute; TOP: 43px "runat =" server "Width =" 162px "Height =" 18px "ErrorMessage =" RequiredFieldValidator "ControlToValidate =" Userid "> enter the logon name! </Asp: RequiredFieldValidator>
<Asp: RequiredFieldValidator id = "RequiredFieldValidator2" style = "Z-INDEX: 108; LEFT: 292px; POSITION: absolute; TOP: 83px "runat =" server "Width =" 175px "Height =" 22px "ErrorMessage =" RequiredFieldValidator "ControlToValidate =" Pwd "> enter the logon password! </Asp: RequiredFieldValidator>
</Form>
</FONT>
</Body>
</HTML>

3. Compile the default. aspx. cs file.
Double-click LogButton,
1. Add using System. Data. OleDb;
2. First declare in class:
Public string strConnection;
OleDbConnection myConn;
3. Join the database link:
Add the following code to "InitializeComponent ();" of "Page_Init (object sender, EventArgs e.
String strConnection = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Server. MapPath (".") + "... user. mdb ;";
MyConn = new OleDbConnection (strConnection );
4. Add the following code to the LogButton_Click (object sender, System. EventArgs e) event:
String userid, pwd;
Userid = Userid. Text;
Pwd = Pwd. Text;
String mySel = "SELECT count (*) as iCount from user where UserID =" "+ userid + """;

OleDbCommand myCmd1 = new OleDbCommand (mySel, myConn );
Mydomain1.connection. Open ();
OleDbDataReader Dr1;
Dr1 = mydesk1.executereader ();
Dr1.Read ();
String Count = Dr1 ["iCount"]. ToString ();
Dr1.Close ();
Mydomain1.connection. Close ();
String DrPwd, DrRoles;
If (Count! = "0 ")
{
MySel = "SELECT * from user where UserID =" "+ userid + """;
OleDbCommand myCmd = new OleDbCommand (mySel, myConn );
MyCmd. Connection. Open ();
OleDbDataReader Dr;
Dr = myCmd. ExecuteReader ();
Dr. Read ();
DrPwd = Dr ["Password"]. ToString ();
Dr. Close ();
If (DrPwd = pwd)
 
Else
Msg. Text = "the logon password is incorrect .";
}
Else
Msg. Text = "this user does not exist .";

 

Now, all the work has been completed. The source code of default. aspx. cs is as follows:

Code

 Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using System. Data. OleDb;


Namespace lsj
{
/// <Summary>
/// Summary description for WebForm1.
/// </Summary>
Public class WebForm1: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. Label Label1;
Protected System. Web. UI. WebControls. Label Label2;
Protected System. Web. UI. WebControls. TextBox Userid;
Protected System. Web. UI. WebControls. Button LogButton;
Protected System. Web. UI. WebControls. TextBox Pwd;
Protected System. Web. UI. WebControls. Label Msg;
Protected System. Web. UI. HtmlControls. HtmlForm Form1;
Protected System. Web. UI. WebControls. RequiredFieldValidator rfvUserid;
Protected System. Web. UI. WebControls. RequiredFieldValidator rfvPwd;
Public string strConnection;
OleDbConnection myConn;

Public WebForm1 ()
{
Page. Init + = new System. EventHandler (Page_Init );
}


Private void Page_Load (object sender, System. EventArgs e)
  


Private void Page_Init (object sender, EventArgs e)
{
InitializeComponent ();
String strConnection = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Server. MapPath (".") + "... user. mdb ;";
// Put user. mdb in the same directory as the aspx File
MyConn = new OleDbConnection (strConnection );
}


Private void InitializeComponent ()
{
This. LogButton. Click + = new System. EventHandler (this. LogButton_Click );
This. Load + = new System. EventHandler (this. Page_Load );
}

Private void LogButton_Click (object sender, System. EventArgs e)
{
String userid, pwd;
Userid = Userid. Text;
Pwd = Pwd. Text;
String mySel = "SELECT count (*) as iCount from user where UserID =" "+ userid + """;

OleDbCommand myCmd1 = new OleDbCommand (mySel, myConn );
Mydomain1.connection. Open ();
OleDbDataReader Dr1;
Dr1 = mydesk1.executereader ();
Dr1.Read ();
String Count = Dr1 ["iCount"]. ToString ();
Dr1.Close ();
Mydomain1.connection. Close ();
String DrPwd, DrRoles;
If (Count! = "0 ")
{
MySel = "SELECT * from user where UserID =" "+ userid + """;
OleDbCommand myCmd = new OleDbCommand (mySel, myConn );
MyCmd. Connection. Open ();
OleDbDataReader Dr;
Dr = myCmd. ExecuteReader ();
Dr. Read ();
DrPwd = Dr ["Password"]. ToString ();
Dr. Close ();
If (DrPwd = pwd)
 
Else
Msg. Text = "the logon password is incorrect .";
}
Else
Msg. Text = "this user does not exist .";
}
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.