Ajax动态加载数据库示例
程序员文章站
2023-11-13 17:40:40
复制代码 代码如下:
<!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>
<title></title>
<script type ="text/javascript">
function btnclick() {
var xmlhttp = xmlhttp = new activexobject("microsoft.xmlhttp");
if (!xmlhttp) {
alert("创建xmlhttp对象异常!");
return false;
}
var text1 = document.getelementbyid("text1");
xmlhttp.open("post","getprice2.ashx?ts"+text1, false);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate == 4) {
if (xmlhttp.status == 200) {
document.getelementbyid("text2").value = xmlhttp.responsetext;
}
else {
alert("ajax返回错误!");
}
}
}
xmlhttp.send();
}
</script>
</head>
<body>
<p>
产品名称:<input id="text1" type="text" /></p>
<p>
价格:<input id="text2" type="text" /></p>
<p>
<input id="button1" type="button" value="查询" onclick = "btnclick()"/></p>
</body>
</html>
<%@ webhandler language="c#" class="getprice" %>
using system;
using system.linq;
using system.web;
using datasetproductstableadapters;
public class getprice : ihttphandler {
public void processrequest (httpcontext context)
{
context.response.contenttype = "text/plain";
//context.response.write("hello world");
string name = context.request["text1"];
var data = new pricetableadapter().getdatabyname(name);//需要建一个强类型的dataset
if (data.count <= 0)
{
context.response.write("none|0");
}
else
{
context.response.write("ok|" + data.single().price);
}
}
public bool isreusable {
get {
return false;
}
}
}
复制代码 代码如下:
<!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>
<title></title>
<script type ="text/javascript">
function btnclick() {
var xmlhttp = xmlhttp = new activexobject("microsoft.xmlhttp");
if (!xmlhttp) {
alert("创建xmlhttp对象异常!");
return false;
}
var text1 = document.getelementbyid("text1");
xmlhttp.open("post","getprice2.ashx?ts"+text1, false);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate == 4) {
if (xmlhttp.status == 200) {
document.getelementbyid("text2").value = xmlhttp.responsetext;
}
else {
alert("ajax返回错误!");
}
}
}
xmlhttp.send();
}
</script>
</head>
<body>
<p>
产品名称:<input id="text1" type="text" /></p>
<p>
价格:<input id="text2" type="text" /></p>
<p>
<input id="button1" type="button" value="查询" onclick = "btnclick()"/></p>
</body>
</html>
复制代码 代码如下:
<%@ webhandler language="c#" class="getprice" %>
using system;
using system.linq;
using system.web;
using datasetproductstableadapters;
public class getprice : ihttphandler {
public void processrequest (httpcontext context)
{
context.response.contenttype = "text/plain";
//context.response.write("hello world");
string name = context.request["text1"];
var data = new pricetableadapter().getdatabyname(name);//需要建一个强类型的dataset
if (data.count <= 0)
{
context.response.write("none|0");
}
else
{
context.response.write("ok|" + data.single().price);
}
}
public bool isreusable {
get {
return false;
}
}
}
下一篇: jquery解析JSON数据示例代码