前端代码:login.aspx
后台代码 :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Login Page</title>
<script src="jQuery.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#btnLogin").click(function(){
Login();
});
});
function Login(){
if(Check())
{
LoginSuccess();
}
}
function Check(){
if($("#txtUser").val()=="")
{
alert("The user isn't empty");
$("#txtUser").focus();
return false;
}
if($("#txtPassword").val()=="")
{
alert("The Password isn't empty")
$("#txtPassword").focus();
return false;
}
return true;
}
function LoginSuccess(){
$.ajax({
type:"POST",
url:"CheckLogin.aspx",
data:{userName:$("#txtUser").val(),userPwd:$("#txtPassword").val()},
beforeSend:function(){$("#msg").html("logining");},
success:function(data){
$("#msg").html(decodeURI(data));
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="margin-left:32px;">
<a>User:</a><input id="txtUser" type="text" />
</div>
<div>
<a>Password:</a><input id="txtPassword" type="text" />
</div>
<div id="msg"></div>
<div>
<input id="btnLogin" type="button" value="Login" /></div>
</form>
</body>
</html>
后台代码 :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class checkLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string retVal = "";
string userName = Request["userName"];
string userPwd = Request["userPwd"];
if (userName == "ike" && userPwd == "123")
{
retVal = "Login is success";
}
else
{
retVal = "Login is false";
}
Response.Write(retVal);
}
}
}