Ajax + asp.net for User Login

Source: Internet
Author: User

Ajax + asp.net for User Login

This article mainly introduces Ajax + asp.net to Implement User Login. It is mainly used to practice ajax usage. If you need it, refer to it.

 

 

Using user logon as an example to practice ajax usage

Login.html

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

Success <! DOCTYPE html>

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> </title>

<Script type = "text/javascript">

Var obj = createobj ();

 

Function login (name, pwd)

{

Var urlstr = "http: /localhost: 14248/server. aspx? Username = "+ name +" & password = "+ pwd;

Obj. open ("get", urlstr, true );

Obj. onreadystatechange = dowork;

Obj. send ();

}

 

Function dowork ()

{

If (obj. readyState = 4)

{

If (obj. status = 200)

{

Document. getElementById ("msg"). innerText = obj. responseText;

}

}

}

// Create an object

Function createobj ()

{

Var xmlHttp = null;

Try {

// Non-IE browser

XmlHttp = new XMLHttpRequest ();

}

Catch (e)

{// IE browser

Try {

XmlHttp = new ActiveXObject ("Msxml2.HTTP ");

}

Catch (e)

{

XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");

}

}

Return xmlHttp;

}

</Script>

</Head>

<Body>

<Table>

<Tr>

<Td align = "center" colspan = "2"> logon </td>

</Tr>

<Tr>

<Td> User name: </td>

<Td> <input type = "text" id = "username" name = "username"/> </td>

</Tr>

<Tr>

<Td> password: </td>

<Td> <input type = "password" id = "password" name = "password"/> </td>

</Tr>

<Tr>

<Td>

<Input type = "submit" value = "login" onclick = "login (document. getElementById ('username '). value, document. getElementById ('Password '). value) "/>

</Td>

<Td>

<Input type = "reset" value = "clear"/>

</Td>

<Td> <span id = "msg"> </span> </td>

</Tr>

</Table>

</Body>

</Html>

DAL. cs

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Using System;

Using System. Collections. Generic;

Using System. Data;

Using System. Data. SqlClient;

Using System. Linq;

Using System. Web;

 

Namespace AJAXtest

{

Public class DAL

{

Private string connstr = "server = acer-pc; database = mydatabase; user id = sa; password = 123456 ";

Public DataTable selectDB (string SQL)

{

DataTable dt = new DataTable ();

Try

{

SqlConnection conn = new SqlConnection (connstr );

SqlDataAdapter sda = new SqlDataAdapter (SQL, conn );

Sda. Fill (dt );

}

Catch (Exception e)

{}

Return dt;

}

}

}

BLL. cs

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Using System;

Using System. Collections. Generic;

Using System. Data;

Using System. Linq;

Using System. Web;

 

Namespace AJAXtest

{

Public class BLL

{

Public bool login (string username, string password)

{

Try

{

String SQL = "select password from Users where username = '" + username + "'";

DAL sqlSelect = new DAL ();

DataTable dt = sqlSelect. selectDB (SQL );

If (dt. Rows [0] ["password"]. ToString ()! = Password)

Return false;

}

Catch (Exception)

{

}

Return true;

}

}

}

Server. aspx. cs

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Web;

Using System. Web. UI;

Using System. Web. UI. WebControls;

 

Namespace AJAXtest

{

Public partial class Server: System. Web. UI. Page

{

Protected void Page_Load (object sender, EventArgs e)

{

String username = Request ["username"]. ToString ();

String password = Request ["password"]. ToString ();

BLL B = new BLL ();

If (B. login (username, password ))

{

Response. Write ("Logon successful ");

Response. End ();

}

Else

{

Response. Write ("Logon Failed ");

Response. End ();

}

}

}

}

Server. aspx

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Dependencies <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Server. aspx. cs" Inherits = "AJAXtest. Server" %>

 

<! DOCTYPE html>

 

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> </title>

</Head>

<Body>

<Form id = "form1" runat = "server">

<Div>

 

</Div>

</Form>

</Body>

</Html>

The above is all the content of this article. I hope you will like it.

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.