刚刚调试完树上的一点代码,感觉Ajax真是个好东西,web应用程序做的越来越象Windows用用程序了,哈哈!
1,新建项目AjaxLoginText项目
2,新建文件夹bin
3,将AjaxPro.dll添加引用到bin文件中
4,建立web.config文件
1
<system.web>
2
<httpHandlers>
3
<add verb="POST,GET" path="AjaxPro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro"/>
4
</httpHandlers>
5
</system.web>
6![]()
<system.web>2
<httpHandlers>3
<add verb="POST,GET" path="AjaxPro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro"/>4
</httpHandlers>5
</system.web>6

这一步配置的主要作用是保证客户端向"ajax/*.ashx"的请求(post和get)都被AjaxPro.AjaxHandlerFactory拦截.
5,新建Verify.aspx页面,并写代码,代码如下:
Verify.aspx 代码:
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Verify.aspx.cs" Inherits="Verify" %>
2![]()
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4![]()
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>AjaxLoginTestDemo</title>
8
<script language="javascript">
9
function VerifyUserName(name)
10
{
11
Verify.GetReturnCode(name,IsUserNameExist_callback);
12
}
13
function IsUserNameExist_callback(res)
14
{
15
var msg=document.getElementById('lbMessage');
16
var bRet=res.value;
17
if(bRet=="0")
18
{
19
msg.innerHTML='用户名存在!!!';
20
msg.style.color='green';
21
}
22
else if(bRet=="1")
23
{
24
msg.innerHTML='用户名输入不合法,长度必须在3-15之间';
25
}
26
else
27
{
28
msg.innerHTML='用户名不存在';
29
msg.style.color='red';
30
}
31
}
32
</script>
33
</head>
34
<body>
35
<form id="form1" runat="server">
36
<div>
37
<asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
38
<asp:TextBox ID="tbUserName" runat="server" onkeyup="VerifyUserName(this.value)"></asp:TextBox><br />
39
<asp:Label ID="lbMessage" runat="server" Text="Label"></asp:Label></div>
40
</form>
41
</body>
42
</html>
43![]()
44![]()
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Verify.aspx.cs" Inherits="Verify" %>2

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head runat="server">7
<title>AjaxLoginTestDemo</title>8
<script language="javascript">9
function VerifyUserName(name)10
{11
Verify.GetReturnCode(name,IsUserNameExist_callback);12
}13
function IsUserNameExist_callback(res)14
{15
var msg=document.getElementById('lbMessage');16
var bRet=res.value;17
if(bRet=="0")18
{19
msg.innerHTML='用户名存在!!!';20
msg.style.color='green';21
}22
else if(bRet=="1")23
{24
msg.innerHTML='用户名输入不合法,长度必须在3-15之间';25
}26
else27
{28
msg.innerHTML='用户名不存在';29
msg.style.color='red';30
}31
}32
</script>33
</head>34
<body>35
<form id="form1" runat="server">36
<div>37
<asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>38
<asp:TextBox ID="tbUserName" runat="server" onkeyup="VerifyUserName(this.value)"></asp:TextBox><br />39
<asp:Label ID="lbMessage" runat="server" Text="Label"></asp:Label></div>40
</form>41
</body>42
</html>43

44

Verify.aspx.cs 代码:
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.Text.RegularExpressions;
12![]()
13
public partial class Verify : System.Web.UI.Page
14
{
15
protected void Page_Load(object sender, EventArgs e)
16
{
17
AjaxPro.Utility.RegisterTypeForAjax(typeof(Verify));
18
}
19![]()
20
[AjaxPro.AjaxMethod]
21
public string GetReturnCode(string strUserName)
22
{
23
if(!IsValidUserName(strUserName))
24
{
25
return "1";
26
}
27
else if(!IsUserNameExist(strUserName))
28
{
29
return "2";
30
}
31
else
32
{
33
return "0";
34
}
35![]()
36
}
37![]()
38
private bool IsUserNameExist(string strUserName)//查询虚拟数据库中是否存在用户名
39
{
40
bool bRet = false;
41
switch (strUserName.ToLower())
42
{
43
case "gm":
44
case "gg":
45
case "mm":
46
case "gaomin":
47
bRet = true;
48
break;
49
}
50
return bRet;
51
}
52
private bool IsValidUserName(string strUserName)//验证输入是否合法
53
{
54
return (Regex.IsMatch(strUserName, @"^(\w{3,15})$"));
55
}
56
}
57![]()
58![]()
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.Text.RegularExpressions;12

13
public partial class Verify : System.Web.UI.Page14
{15
protected void Page_Load(object sender, EventArgs e)16
{17
AjaxPro.Utility.RegisterTypeForAjax(typeof(Verify));18
}19

20
[AjaxPro.AjaxMethod]21
public string GetReturnCode(string strUserName)22
{ 23
if(!IsValidUserName(strUserName))24
{25
return "1";26
}27
else if(!IsUserNameExist(strUserName))28
{29
return "2";30
}31
else32
{33
return "0";34
}35

36
}37

38
private bool IsUserNameExist(string strUserName)//查询虚拟数据库中是否存在用户名39
{40
bool bRet = false;41
switch (strUserName.ToLower())42
{ 43
case "gm":44
case "gg":45
case "mm":46
case "gaomin":47
bRet = true;48
break;49
}50
return bRet;51
}52
private bool IsValidUserName(string strUserName)//验证输入是否合法53
{54
return (Regex.IsMatch(strUserName, @"^(\w{3,15})$"));55
}56
}57

58


浙公网安备 33010602011771号