Ajax的注册小示例
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11
12 /// <summary>
13 /// Connection 的摘要说明
14 /// </summary>
15 public class Connection
16 {
17 public Connection()
18 {
19 //
20 // TODO: 在此处添加构造函数逻辑
21 //
22 }
23
24 public static SqlConnection getConnection()
25 {
26 return new SqlConnection (System .Configuration .ConfigurationManager .ConnectionStrings ["ConnectionString"].ConnectionString );
27 }
28 }
29
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11
12 /// <summary>
13 /// User 的摘要说明
14 /// </summary>
15 public class User
16 {
17 public User()
18 {
19 //
20 // TODO: 在此处添加构造函数逻辑
21 //
22 }
23
24 /// <summary>
25 /// 检测用户是否存在
26 /// </summary>
27 /// <param name="userName">用户名</param>
28 /// <returns>bool</returns>
29 public bool checkName(string userName)
30 {
31 SqlConnection userConnection = Connection.getConnection();
32 SqlCommand userCommand = new SqlCommand("checkName", userConnection);
33
34 userCommand.CommandType = CommandType.StoredProcedure;
35 userCommand.Parameters.Add("@userName", SqlDbType.VarChar, 50);
36 userCommand.Parameters["@userName"].Value = userName;
37 userCommand.Connection.Open();
38 int n = (int)userCommand.ExecuteScalar();
39 userCommand.Connection.Close();
40
41 if (n > 0)
42 {
43 return true;
44 }
45 else
46 {
47 return false;
48 }
49 }
50
51 /// <summary>
52 /// 插入用户
53 /// </summary>
54 /// <param name="userName">用户名</param>
55 /// <param name="userPwd">密码</param>
56 /// <returns></returns>
57 public bool insertUserInfo(string userName, string userPwd)
58 {
59 SqlConnection userConnection = Connection.getConnection();
60 SqlCommand userCommand = new SqlCommand("insertUserInfo", userConnection);
61
62 userCommand.CommandType = CommandType.StoredProcedure;
63 userCommand.Parameters.Add("@userName", SqlDbType.VarChar, 50);
64 userCommand.Parameters["@userName"].Value = userName;
65
66 userCommand.Parameters.Add("@userPwd", SqlDbType.VarChar, 50);
67 userCommand.Parameters["@userPwd"].Value = userPwd;
68
69 userCommand.Connection.Open();
70 userCommand.ExecuteNonQuery();
71
72 userCommand.Connection.Close();
73 return true;
74 }
75
76 /// <summary>
77 /// 查询用户信息
78 /// </summary>
79 /// <returns>SqlDataReader</returns>
80 public SqlDataReader selectUserInfo()
81 {
82 SqlConnection userConnection = Connection.getConnection();
83 SqlCommand userCommand = new SqlCommand("select * from UserInfo",userConnection );
84 userCommand.Connection.Open() ;
85 return userCommand.ExecuteReader();
86
87 }
88
89 /// <summary>
90 /// 删除用户
91 /// </summary>
92 /// <param name="userName">用户名</param>
93 /// <returns>bool</returns>
94 public bool deleteUser(string userName)
95 {
96 SqlConnection userConnection = Connection.getConnection();
97 SqlCommand userCommand = new SqlCommand("deleteUser", userConnection);
98 userCommand.CommandType = CommandType.StoredProcedure;
99
100 userCommand.Parameters.Add("@userName", SqlDbType.VarChar, 50);
101 userCommand.Parameters["@userName"].Value = userName;
102
103 userCommand.Connection.Open();
104
105 userCommand.ExecuteNonQuery();
106 userCommand.Connection.Close();
107
108 return true;
109 }
110
111 /// <summary>
112 /// 更新用户信息
113 /// </summary>
114 /// <param name="userName"></param>
115 /// <param name="userPwd"></param>
116 /// <returns></returns>
117 public bool updateUserInfo(string userName, string userPwd)
118 {
119 SqlConnection userConnection = Connection.getConnection();
120 SqlCommand userCommand = new SqlCommand("updateUserInfo", userConnection);
121 userCommand.CommandType = CommandType.StoredProcedure;
122 userCommand.Parameters.Add("@userName", SqlDbType.VarChar, 50);
123 userCommand.Parameters["@userName"].Value = userName;
124 userCommand.Parameters.Add("@userPwd", SqlDbType.VarChar, 50);
125 userCommand.Parameters["@userPwd"].Value = userPwd;
126
127 userCommand.Connection.Open();
128 userCommand.ExecuteNonQuery();
129 userCommand.Connection.Close();
130
131 return true;
132 }
133 }
134
1

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>2

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4
<html xmlns="http://www.w3.org/1999/xhtml">5
<head runat="server">6
<title></title>7
<script language ="javascript" src="CreateObject.js"></script>8
</head>9
<body>10
<form id="form1" runat="server">11
<div style="text-align: center">12
<table style="width: 420px; height: 256px; text-align: center;" id="TABLE1" onclick="return TABLE1_onclick()">13
14
<tr>15
<td style="width: 69px; height: 21px; border: 1px; text-align: center;">16
<asp:Label ID="lblUserName" runat="server" Text="用户名:"></asp:Label></td>17
<td style="width: 69px; height: 21px; border: 1px;">18
<input id="txtUserName" type="text" onkeyup="CheckName(document.getElementById('txtUserName').value);" /></td>19
<td>20
<img id="imgName" src="" /></td>21
</tr>22
<tr>23
<td style="width: 69px; height: 21px; border: 1px;">24
<asp:Label ID="lblUserPwd" runat="server" Text="密码:"></asp:Label></td>25
<td style="width: 69px; height: 21px; border: 1px;">26
<input id="txtUserPwd" type="text" /></td>27
<td>28
</td>29
</tr>30
<tr>31
<td style="width: 69px; height: 21px; border: 1px;">32
<asp:Label ID="lblUserPwdRe" runat="server" Text="重复密码:"></asp:Label></td>33
<td style="width: 69px; height: 21px; border: 1px;">34
<input id="txtUserPwdRe" type="text" />35
</td>36
<td>37
</td>38
</tr>39
<tr>40
<td colspan="3" style="border-top-width: 1px; border-left-width: 1px; border-bottom-width: 1px;41
height: 21px; border-right-width: 1px">42
<input id="btnOk" style="width: 90px" type="button" value="OK" /></td>43
</tr>44
</table>45
</div>46
</form>47
</body>48
</html>49

1
// JScript 文件2
var xmlHttp;3

4
//检测用户是否存在5
function CheckName(userName)6


{7
createXMLHTTP();8
var url="DisposeEvent.aspx?Name="+userName+"&Event=Check";9
xmlHttp.open("GET",url,true);10
xmlHttp.onreadystatechange=checkUserName;11
xmlHttp.send(null);12
}13

14
function createXMLHTTP()15


{16
if(window.XMLHttpRequest)17

{18
xmlHttp = new XMLHttpRequest();19
}20
else if(window.ActiveXObject)21

{22
try23

{24
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");25
}26
catch(e)27

{}28
29
try30

{31
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");32
}33
catch(e)34

{}35
36
if(!xmlHttp)37

{38
window.alert("不能创建XMLHttpRequest");39
return false;40
}41
}42
}43

44
function checkUserName()45


{46
if(xmlHttp.readyState == 4)47

{48
if(xmlHttp.status==200)49

{50
if(xmlHttp.responseText == "true")51

{52
document.getElementById("imgName").src="images/true.gif";53
54
//注册按钮=false55
document.getElementById("btnOk").disabled = false;56
}57
else 58

{59
document.getElementById("imgName").src="images/flase.gif";60
document.getElementById("btnOk").disabled = true;61
}62
}63
}64
}65

66

67
//注册新用户68
function regUser()69


{70
if(document.getElementById("txtUserName").value=="")71

{72
alert("对不起,用户名不能为空!");73
return false;74
}75
76
if(document.getElementById("txtUserPwd").value="")77

{78
alert("对不起,密码不能为空");79
return false;80
}81
82
var url="DisposeEvent.aspx?Name="+document.getElementById("userName").value+"&Pwd="+document.getElementById("txtUserPwd").value+"&Event=Reg";83
xmlHttp.open("GET",url,true);84
xmlHttp.onreadystatechange=regUserInfo;85
xmlHttp.send(null);86
}87

88
//注册用户回调函数89
function regUserInfo()90


{91
if(xmlHttp.readyState==4)92

{93
if(xmlHttp.status == 200)94

{95
if(xmlHttp.responseText=="true")96

{97
alert("恭喜,新用户注册成功。");98
document.getElementById("txtUserName").value="";99
document.getElementById("txtUserPwd").value="";100
}101
else102

{103
alert("对不起,注册失败");104
document.getElementById("txtUserName").value="";105
document.getElementById("txtUserPwd").value="";106
}107
}108
}109
}110

111
1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11
using System.Data.SqlClient;12

13
public partial class DisposeEvent : System.Web.UI.Page14


{15
protected void Page_Load(object sender, EventArgs e)16

{17
User user= new User();18

19
//是否为执行checkName的方法20
if (Request.QueryString["Event"].ToString() == "Check")21

{22
if (user.checkName(Request.QueryString["Name"].ToString()))23

{24
//当数据库已经存在此用户25
Response.Write("false");26
Response.End();27
}28
else29

{30
Response.Write("true");31
Response.End();32
}33
}34

35
//是否为执行regUser方法36
if (Request.QueryString["Event"].ToString() == "Reg")37

{38
if (user.insertUserInfo(Request.QueryString["Name"].ToString(), Request.QueryString["Pwd"].ToString()))39

{40
Response.Write("true");41
Response.End();42
}43
}44
}45

46
}47

1
set ANSI_NULLS ON
2
set QUOTED_IDENTIFIER ON
3
go
4![]()
5
ALTER PROCEDURE [dbo].[checkName]
6
@userName varchar(50)
7
AS
8
SELECT COUNT(*) FROM UserInfo
9
WHERE UserName=@userName
10![]()
set ANSI_NULLS ON2
set QUOTED_IDENTIFIER ON3
go4

5
ALTER PROCEDURE [dbo].[checkName]6
@userName varchar(50)7
AS8
SELECT COUNT(*) FROM UserInfo9
WHERE UserName=@userName10

1
set ANSI_NULLS ON
2
set QUOTED_IDENTIFIER ON
3
go
4![]()
5
ALTER PROCEDURE [dbo].[deleteUser]
6
@userName varchar(50)
7
AS
8
DELETE FROM UserInfo WHERE UserName=@userName
9
return
10![]()
set ANSI_NULLS ON2
set QUOTED_IDENTIFIER ON3
go4

5
ALTER PROCEDURE [dbo].[deleteUser]6
@userName varchar(50)7
AS8
DELETE FROM UserInfo WHERE UserName=@userName9
return10

1 set ANSI_NULLS ON
2 set QUOTED_IDENTIFIER ON
3 go
4
5 ALTER PROCEDURE [dbo].[insertUserInfo]
6 @userName varchar(50),
7 @userPwd varchar(50)
8 AS
9 INSERT INTO UserInfo VALUES(@userName,@userPwd)
10 return
11
12
1 set ANSI_NULLS ON
2 set QUOTED_IDENTIFIER ON
3 go
4
5 ALTER PROCEDURE [dbo].[updateUserInfo]
6 @userName varchar(50),
7 @userPwd varchar(50)
8 AS
9 UPDATE UserInfo SET UserPwd=@userPwd
10 WHERE UserName=@userName
11 return
12
13

浙公网安备 33010602011771号