前几天把jbox源码修改成仿QQ空间模拟窗口后发现有很多人在关注。今天就贴一下我利用该模拟窗口实现的用户登录功能的代码。
ok,先来贴几张张效果图。
![](https://images.cnblogs.com/cnblogs_com/fishbin/article/TrainLoginDemo.png)
![](https://images.cnblogs.com/cnblogs_com/fishbin/article/divbox2.png)
其中大致流程是用户点击页面右上角的登录链接接着弹出div模拟窗口,该窗口通过iframe调用Login.aspx页面,用户输入用户名
密码和验证码后,Login.aspx页面的jQuery代码post到Login.ashx页面处理,Login.ashx页面可以算是简易的aspx页面吧。
当然你用LoginProcess.aspx 也是可以的。Login.ashx页面处理完把结果返回给Login.aspx页面处理,result变量用与接收结果。
如果返回1表示登录成功,则关闭模拟窗口。
主页面调用代码片段:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
<asp:HyperLink ID="lnkLogin" runat="server" NavigateUrl="#" >登录</asp:HyperLink>
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
<script language="javascript" type="text/javascript">
4
$('#<%=this.lnkLogin.ClientID %>').click(
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function()
{
6
jBox.open('iframe-jBoxID','iframe','Login.aspx','用户登录
7
','width=400,height=250,center=true,draggable=true,model=true');
8
} );
9
</script>
Login.aspx代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
<form id="form1" onsubmit="return false;">
2
<table id="login-table">
3
<tr>
4
<td width="60">学号:</td>
5
<td><input class="textbox" type="text" style="width:160px;" id="txtUserName"
6
maxlength="9" onblur="checkUserName()" onclick="$.trim(this.value)"/><span></span>
7
</td>
8
</tr>
9
<tr>
10
<td width="60">密码:</td>
11
<td><input class="textbox" type="password" style="width:160px;" id="txtUserPwd"
onblur="checkUserPwd()" onclick="$.trim(this.value)" /><span></span>
12
</td>
13
</tr>
14
<tr>
15
<td width="60">验证码:</td>
16
<td><input class="textbox" type="text" style="width:160px;" maxlength="5"
17
id="txtCheckCode" onblur="checkCheckCode()" onclick="$.trim(this.value)"/><span>
18
</span>
19
</td>
20
</tr>
21
<tr>
22
<td width="60"></td>
23
<td><div style="color:#808080;">输入下图中的字符,不区分大小写</div><br />
24
<img src="CheckCode.aspx" style="vertical-align:middle;" alt="验证码" id="imgCheckCode" />
25
<a href="#" id="change_image">看不清,换一张</a></td>
26
</tr>
27
<tr>
28
<td width="60"></td>
29
<td><input type="image" src="App_Themes/Images/btn_login.jpg" id="btnLogin"
30
alt="马上登录" style="border:0;"/></td>
31
</tr>
32
</table>
33
</form>
jQuery代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="javascript" type="text/javascript" >![](https://www.cnblogs.com/Images/dot.gif)
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
$(document).ready(function()
{
3
// 验证码更新
4
$('#change_image').click(
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function()
{
6
$('#imgCheckCode').attr('src','CheckCode.aspx?'+Math.random());
7
});
8
//关键的代码
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
$("#btnLogin").click(function()
{
10
if(checkUserName() && checkUserPwd() && checkCheckCode())
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var data =
{
13
UserName: $('#txtUserName').val(),
14
UserPwd: $('#txtUserPwd').val(),
15
CheckCode: $('#txtCheckCode').val()
16
};
17
//提交数据给Login.ashx页面处理
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
$.post("Ajax/Login.ashx",data,function(result)
{
19
if(result == "1") //登录成功
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
alert("登录成功!您可以进行其他操作了!");
22
// 关闭模拟窗口
23
window.parent.window.jBox.close();
24
}
25
else if(result == "2") //验证码错误
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
$('#txtCheckCode').next("span").css("color","red").text("*
28
验证码错误");
29
}
30
else
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
alert("登录失败!请重试");
33
}
34
});
35
}
36
else
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
checkUserName();
39
checkUserPwd();
40
checkCheckCode();
41
}
42
});
43
});
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
//check the userName
46
function checkUserName()
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
if($("#txtUserName").val().length == 0)
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
$("#txtUserName").next("span").css("color","red").text("*用户名不为空");
51
return false;
52
}
53
else
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
var reg = /^\d{9}$/;
56
if(!reg.test($('#txtUserName').val()))
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
$('#txtUserName').next("span").css("color","red").text("*正确的格式
59
如:030602888");
60
return false;
61
}
62
else
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
$("#txtUserName").next("span").css("color","red").text("");
65
return true;
66
}
67
}
68
}
69
//check the pwd
70
function checkUserPwd()
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
if($('#txtUserPwd').val().length == 0)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
$('#txtUserPwd').next("span").css("color","red").text("*密码不为空");
75
return false;
76
}
77
else
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
$('#txtUserPwd').next("span").css("color","red").text("");
80
return true;
81
}
82
}
83
// check the check code
84
function checkCheckCode()
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
if($('#txtCheckCode').val().length == 0)
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
$('#txtCheckCode').next("span").css("color","red").text("*验证码不为空");
89
return false;
90
}
91
else
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
$('#txtCheckCode').next("span").css("color","red").text("");
94
return true;
95
}
96
}
97
</script>
Login.ashx代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections;
3
using System.Data;
4
using System.Linq;
5
using System.Web;
6
using System.Web.Services;
7
using System.Web.Services.Protocols;
8
using System.Xml.Linq;
9
using System.Data.SqlClient;
10
using System.Web.SessionState; //支持session必须的引用
11![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
namespace Website.Ajax
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
14
[WebService(Namespace = "http://tempuri.org/")]
15
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
16
public class Login : IHttpHandler,IRequiresSessionState
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
public void ProcessRequest(HttpContext context)
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
context.Response.ContentType = "text/plain";
21
string checkCode = "";
22
if (context.Session["checkCode"] != null)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
checkCode = Convert.ToString(context.Session["checkCode"]).ToLower();
25
}
26
if (context.Request.Form["CheckCode"].ToLower() == checkCode)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
using (SqlConnection conn = new
29
SqlConnection(SqlHelper.StudentConnectionString))
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
string sql = "select ID,stuNumber,userPassword,realName from
32
t_stuUser where stuNumber=@UserName and userPassword=@UserPwd";
33
SqlCommand cmd = new SqlCommand(sql, conn);
34
SqlParameter pUserName = cmd.Parameters.Add("@UserName",
35
SqlDbType.VarChar, 30);
36
SqlParameter pUserPwd = cmd.Parameters.Add("@UserPwd",
37
SqlDbType.VarChar, 150);
38
pUserName.Value = context.Request.Form["UserName"];
39
pUserPwd.Value = Common.MD5(context.Request.Form["UserPwd"]);
40
conn.Open();
41
SqlDataReader sdr =
42
cmd.ExecuteReader(CommandBehavior.CloseConnection);
43
if (sdr.Read())
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
context.Session["UserID"] = Convert.ToString(sdr["ID"]);
46
context.Session["StuName"] =
47
Convert.ToString(sdr["realName"]);
48
context.Session["StuNumber"] =
49
Convert.ToString(sdr["stuNumber"]);
50
context.Response.Write("1"); // 登录成功
51
}
52
else
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
context.Response.Write("0"); //登录失败,用户名或密码错误
55
}
56
}
57
}
58
else
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
context.Response.Write("2"); // 验证码错误
61
}
62
}
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
public bool IsReusable
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
get
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
return false;
69
}
70
}
71
}
72
}
73![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
欢迎转载,请注明出处