登录案例:

新建网站,在网站中添加一个longth.html(静态网页)文件和Handler.ashx(一般处理程序)文件

1.longth.html文件

HTML代码

<form action="Handler.ashx" method="post"> 注:提交到Handler.ashx(一般处理程序) 请求方式post

账号:<input id="name" name="name" type="text" /><br />

密码:<input id="Password" name="password" type="password" /><br />

<input id="Button1" type="submit" value="登录" /> 注:提交按钮

</form>

jquery代码

<script src="jq/jquery-3.2.1.min.js"></script>

$(function () {

$("form").submit(function () { 注:表单提交事件(submit())

var name = $("#name").val().trim();

var password = $("#Password").val().trim();

if(name=="")

{

alert("账号不能为空");

$("#name").focus();

return false; 注:如果账号为空就返回false不提交

}

else if(password=="")

{

alert("密码不能为空");

$("#Password").focus();

return false; 注:如果密码为空就返回false不提交

}

return true; 注:如果都不为空就返回true提交表单

});

});

2.Handler.ashx(一般处理程序)

string account = context.Request["account"]; 注:获得name属性为account的文本框里的值
string password = context.Request["password"]; 注:获得name属性为password的文本框里的值
if (account == "" && password == "")
{
context.Response.Write("账号或密码为空"); 注;如果account或password等于空就弹出账号或密码为空
}
string sql = "select COUNT(*) from name where name=@name and password=@password"; 注:sql语句
SqlParameter[] sp = {
new SqlParameter("@name",account),
new SqlParameter("@password",password)
};
int i = Convert.ToInt32(SQLHelper.ExecuteScalar(sql, sp)); 注:调用SQLHelper类里的ExecuteScalar方法然后转换为int类型
if (i == 1)
{
context.Response.Write("登录成功");  注:如果查询的数距有就返回1,弹出登录成功
}
else
{
context.Response.Write("登录失败"); 注:如果查询的数距没有,弹出登录失败
}