一.判断账户密码
《Login.html》
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Script/jquery.js"></script>
<script language="javascript">
$(document).ready(function () {
$("#Submit1").click(function () {
//点击 获取 txtUID txtPWD 的值
var u = $("#txtUID").val();
var p = $("#txtPWD").val(); //send request
$.ajax({
url: "Login.aspx",
//将获取的值 传入login.asox
//data(name,值)
data:{uid:u,pwd:p},
type:"POST",
dataType:"Text",
success: function (data) {
//接受传过来的值 判断数据 是否存在
if (data == "1") { //登录成功
$("#d1").css("display", "none");
$("#d2").css("display", "block");
$("#ss").text(u);
}
else { //登录失败
alert("错了");
}
}
}); return false;
}); }); </script>
<meta charset="utf-8" />
</head>
<body> <form id="f1" action="Login.aspx" method="get">
<div id="d1">
账号:<input name="txtUID" id="txtUID" type="text" /><br />
密码:<input name="txtPWD" id="txtPWD" type="text" /><br />
<input id="Submit1" type="submit" value="提交" />
</div>
<div id="d2" style="display:none;">
欢迎您,<span id="ss"></span>
</div>
</form>
</body>
《Lojin.aspx》后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Login : System.Web.UI.Page
{
string[] u = { "aaa","bbb","ccc"};
string[] p = { "aaa", "bbb", "ccc" };
protected void Page_Load(object sender, EventArgs e)
{
string uid = Request["uid"].ToString();
string pwd = Request["pwd"].ToString();
//string uid = Request.QueryString["txtUID"].ToString();
//string pwd = Request.QueryString["txtPWD"].ToString();
//string uid = Request.Form["txtUID"].ToString();
//string pwd = Request.Form["txtPWD"].ToString();
//Response.Write(uid+"<br>"+pwd);
//contains 包含关系
if (u.Contains(uid) == true && p.Contains(pwd))
{
Response.Write("1");
}
else
{
Response.Write("0");
}
Response.End();
}
}
二.调取数据模糊查询
《Shownation.html》
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Script/jquery.js"></script>
<script language="javascript">
$(document).ready(function () {
$("#b").click(function () {
//取文本框的值
var s = $("#n").val();
//查询显示
$.ajax({
url: "Shownation.ashx",
data: {name:s},
type: "GET",
dataType: "XML",
success: function (data) {
var cars = $(data).find("car");
$.each(cars, function (key,value) {
//解析XML
var code = $(value).attr("code"); //读属性
var name = $(value).find("name").text(); //读子元素
var prod = $(value).find("prod").text();
var brand = $(value).find("brand").text();
var price = $(value).find("price").text();
//显示在界面上 Html
var s = "<tr>"
s += "<td>" + code + "</td>";
s += "<td>" + name + "</td>";
s += "<td>" + prod + "</td>";
s += "<td>" + brand + "</td>";
s += "<td>" + price + "</td>";
s += "</tr>"; $("#dd").append(s);
});
//for (var i = 0; i < cars.length; i++) {
// //解析XML
// var code = $(cars).eq(i).attr("code"); //读属性
// var name = $(cars).eq(i).find("name").text(); //读子元素
// var prod = $(cars).eq(i).find("prod").text();
// var brand = $(cars).eq(i).find("brand").text();
// var price = $(cars).eq(i).find("price").text();
// //显示在界面上 Html
// var s = "<tr>"
// s += "<td>" + code + "</td>";
// s += "<td>" + name + "</td>";
// s += "<td>" + prod + "</td>";
// s += "<td>" + brand + "</td>";
// s += "<td>" + price + "</td>";
// s += "</tr>"; // $("#dd").append(s);
//} }
});
});
});
</script>
<meta charset="utf-8" />
</head>
<body>
<div>
名称:<input type="text" name="n" id="n" /> <input type="button" value="查询" name="b" id="b" />
<table id="dd" border="1" width="100%"></table> </div>
</body>
《Shownation.ashx》
<%@ WebHandler Language="C#" Class="Shownation" %> using System;
using System.Web;
using System.Linq;
using System.Data.Linq;
public class Shownation : IHttpHandler {
private MyDataDataContext _Context = new MyDataDataContext();
public void ProcessRequest (HttpContext context)
//加载的时候 加载context 操作时 context 打头
{
string n = context.Request["name"].ToString();
//判断出入是否 包含
var query = _Context.car.Where(p => p.name.Contains(n));
//Html"<?xml version='1.0'?>"
context.Response.Write("<?xml version='1.0'?>");
context.Response.Write("<root>");
foreach (car data in query)
{//foreach 便利数据库 一条条 构建
context.Response.Write("<car code='"+data.code+"'>");
context.Response.Write("<name>"+data.name+"</name>");
context.Response.Write("<prod>"+data.brand1.productor.prod_name+"</prod>");
context.Response.Write("<brand>"+data.brand1.brand_name+"</brand>");
context.Response.Write("<price>"+data.price+"</price>");
context.Response.Write("</car>");
}
context.Response.Write("</root>");
context.Response.End();
} public bool IsReusable {
get {
return false;
}
} }
三.时钟
《Third.aspx》
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Script/jquery.js"></script>
<script language="javascript">
ShowTime(); function ShowTime() {
//...
//alert("aaaa");
$.ajax({
url: "Second.aspx",
data: {},
type: "POST",
dataType: "Text",
success: function (data) {
$("#Label1").text(data);
}
});
window.setTimeout("ShowTime()", 1000);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="42pt" ForeColor="Red"></asp:Label>
</div>
</form>
</body>
《Second.aspx》
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Second : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString());
Response.End();
}
}