随笔简介:记录自己的学习,免得以后遗忘,测试环境vs2008,sql2005
1.为了测试,1个2个页面,其中service.aspx为提供服务页面,default.apx为界面页面
2.前一篇随笔已经做过太多介绍了,所以这只是自己根据前面所学的,做一个简单实现
3.就一点代码,运行无问题
4.service.aspx代码
//前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="service.aspx.cs" Inherits="WebApplication1.serv" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="service.aspx.cs" Inherits="WebApplication1.serv" %>
//后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
namespace WebApplication1
{
public partial class serv : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["Id"];
SqlConnection con = new SqlConnection();
string s ="Data Source=GMSOFT-0ZEME6EL;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa";
con.ConnectionString = s;
con.Open();
string sql = "select * from orders where shipvia=3 and employeeid="+id;
SqlDataAdapter da = new SqlDataAdapter(sql,con);
DataSet ds=new DataSet();
da.Fill(ds);
con.Close();
string xml = "";
DataTable dt = ds.Tables[0];
foreach (DataRow row in dt.Rows)
{
xml += "
xml += "
xml += "
xml += "
xml += "";
}
xml += "";
Response.ClearContent();
Response.Cache.SetNoStore();
Response.ContentType = "text/xml";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(xml);
}
}
}
5.default.aspx只有前台代码,后台无
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="WebApplication1.fb" %>
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>title>
<script language="javascript">
var xmlHttp;
function SetBList() {
var avalue = document.getElementById("AList").value;
var url = "service.aspx?id=" + avalue;
createXMLHttpRequest(); // 创建xmlHttp对象
xmlHttp.onreadystatechange = handleStateChange; // 当xmlHttp状态码发生改变时,调用handleStateChage方法
xmlHttp.open("GET", url, true); // GET方法发送请求
xmlHttp.send(null);
}
function createXMLHttpRequest()
{ // IE
if (window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} // Mozilla
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200) {
var tag=xmlHttp.responseXML.getElementsByTagName("Order");
var orderId = tag[0].getElementsByTagName("OrderId")[0].text;
var gustomerId = tag[0].getElementsByTagName("CustomerId")[0].text;
document.getElementById("orderId").innerText = orderId;
document.getElementById("customerId").innerText = gustomerId;
}
}
}
script>
head>
<body>
<form id="Form1" method="post" runat="server">
<div>javascript实现前台无刷新div>
请选择雇员编号:
<select id="AList" onchange="SetBList()">
<option value="7">7option>
<option value="3">3option>
<option value="5">5option>
select>
订单编号:<asp:TextBox ID="orderId" runat="server">asp:TextBox>
顾客姓名:<asp:TextBox ID="customerId" runat="server">asp:TextBox>
form>
body>
html>