国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

asp.net jQuery Ajax用戶(hù)登錄功能的實(shí)現(xiàn)

2024-07-10 13:18:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
主頁(yè)面調(diào)用代碼片段:

復(fù)制代碼 代碼如下:


<asp:HyperLink runat="server" NavigateUrl="#" >登錄</asp:HyperLink>
<script language="javascript" type="text/javascript">
$('#<%=this.lnkLogin.ClientID %>').click(
function(){
jBox.open('iframe-jBoxID','iframe','Login.aspx','用戶(hù)登錄
','width=400,height=250,center=true,draggable=true,model=true');
} );
</script>


Login.aspx代碼:

復(fù)制代碼 代碼如下:


<form onsubmit="return false;">
<table>
<tr>
<td>學(xué)號(hào):</td>
<td><input type="text"
maxlength="9" onblur="checkUserName()"/><span></span>
</td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password"
onblur="checkUserPwd()" /><span></span>
</td>
</tr>
<tr>
<td>驗(yàn)證碼:</td>
<td><input type="text" maxlength="5"
id="txtCheckCode" onblur="checkCheckCode()"/><span>
</span>
</td>
</tr>
<tr>
<td></td>
<td><div>輸入下圖中的字符,不區(qū)分大小寫(xiě)</div><br />
<img src="CheckCode.aspx" />
<a href="#">看不清,換一張</a></td>
</tr>
<tr>
<td></td>
<td><input type="image" src="App_Themes/Images/btn_login.jpg"
alt="馬上登錄"/></td>
</tr>
</table>
</form>


jQuery代碼:

復(fù)制代碼 代碼如下:


<script language="javascript" type="text/javascript" >
$(document).ready(function(){
// 驗(yàn)證碼更新
$('#change_image').click(
function(){
$('#imgCheckCode').attr('src','CheckCode.aspx?'+Math.random());
});
//關(guān)鍵的代碼
$("#btnLogin").click(function(){
if(checkUserName() && checkUserPwd() && checkCheckCode())
{
var data = {
UserName: $('#txtUserName').val(),
UserPwd: $('#txtUserPwd').val(),
CheckCode: $('#txtCheckCode').val()
};
//提交數(shù)據(jù)給Login.ashx頁(yè)面處理
$.post("Ajax/Login.ashx",data,function(result){
if(result == "1") //登錄成功
{
alert("登錄成功!您可以進(jìn)行其他操作了!");
// 關(guān)閉模擬窗口
window.parent.window.jBox.close();
}
else if(result == "2") //驗(yàn)證碼錯(cuò)誤
{
$('#txtCheckCode').next("span").css("color","red").text("*
驗(yàn)證碼錯(cuò)誤");
}
else
{
alert("登錄失敗!請(qǐng)重試");
}
});
}
else
{
checkUserName();
checkUserPwd();
checkCheckCode();
}
});
});
//check the userName
function checkUserName()
{
if($("#txtUserName").val().length == 0)
{
$("#txtUserName").next("span").css("color","red").text("*用戶(hù)名不為空");
return false;
}
else
{
var reg = /^/d{9}$/;
if(!reg.test($('#txtUserName').val()))
{
$('#txtUserName').next("span").css("color","red").text("*正確的格式
如:030602888");
return false;
}
else
{
$("#txtUserName").next("span").css("color","red").text("");
return true;
}
}
}
//check the pwd
function checkUserPwd()
{
if($('#txtUserPwd').val().length == 0)
{
$('#txtUserPwd').next("span").css("color","red").text("*密碼不為空");
return false;
}
else
{
$('#txtUserPwd').next("span").css("color","red").text("");
return true;
}
}
// check the check code
function checkCheckCode()
{
if($('#txtCheckCode').val().length == 0)
{
$('#txtCheckCode').next("span").css("color","red").text("*驗(yàn)證碼不為空");
return false;
}
else
{
$('#txtCheckCode').next("span").css("color","red").text("");
return true;
}
}
</script>


Login.ashx代碼:

復(fù)制代碼 代碼如下:


using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.SessionState; //支持session必須的引用
namespace Website.Ajax
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Login : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string checkCode = "";
if (context.Session["checkCode"] != null)
{
checkCode = Convert.ToString(context.Session["checkCode"]).ToLower();
}
if (context.Request.Form["CheckCode"].ToLower() == checkCode)
{
using (SqlConnection conn = new SqlConnection(SqlHelper.StudentConnectionString))
{
string sql = "select ID,stuNumber,userPassword,realName from t_stuUser
where stuNumber=@UserName and userPassword=@UserPwd";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter pUserName = cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 30);
SqlParameter pUserPwd = cmd.Parameters.Add("@UserPwd", SqlDbType.VarChar, 150);
pUserName.Value = context.Request.Form["UserName"];
pUserPwd.Value = Common.MD5(context.Request.Form["UserPwd"]);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (sdr.Read())
{
context.Session["UserID"] = Convert.ToString(sdr["ID"]);
context.Session["StuName"] = Convert.ToString(sdr["realName"]);
context.Session["StuNumber"] = Convert.ToString(sdr["stuNumber"]);
context.Response.Write("1"); // 登錄成功
}
else
{
context.Response.Write("0"); //登錄失敗,用戶(hù)名或密碼錯(cuò)誤
}
}
}
else
{
context.Response.Write("2"); // 驗(yàn)證碼錯(cuò)誤
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 虹口区| 恭城| 永新县| 咸丰县| 上虞市| 洪江市| 泊头市| 太仓市| 华亭县| 阿鲁科尔沁旗| 呼图壁县| 房产| 天镇县| 宿迁市| 土默特右旗| 江川县| 昌宁县| 呼玛县| 信宜市| 固原市| 英山县| 永宁县| 襄城县| 威远县| 滦平县| 宁夏| 金塔县| 高州市| 黔南| 穆棱市| 武冈市| 修武县| 西峡县| 铜山县| 霍州市| 青川县| 镇沅| 额尔古纳市| 潍坊市| 博乐市| 武定县|