<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="Script/jquery-1.7.1.min.js"></script> </head> <body> <form id="form1" runat="server"> <div> <input id="txtcode" type="text" /> <input id="btn" type="button" value="查看" /> <div id="name"></div> <div id="sex"></div> <div id="birthday"></div> <div id="nation"></div> </div> </form> <script type="text/javascript"> $(document).ready(function () { $("#btn").click(function () { //取值 var code = $("#txtcode").val(); //调AJax $.ajax({ url: "Show.ashx", type: "POST", data: { code: code }, datatype: "XML", success: function (data) { $("#name").text($(data).find("Name").eq(0).text()); $("#sex").text($(data).find("Sex").text()); $("#nation").text($(data).find("Nation").text()); $("#birthday").text($(data).find("Birthday").text()); } }); }) }) </script> </body> </html>
<%@ WebHandler Language="C#" Class="Show" %> using System; using System.Web; using System.Data; using System.Linq; using System.Data.Linq; public class Show : IHttpHandler { public void ProcessRequest (HttpContext context) { //取值 string code = context.Request["code"].ToString(); //操作数据库 zxcDataContext _context = new zxcDataContext(); //找到一条Info类型的数据 Info data = _context.Info.Where(p => p.Code == code).First(); //返回XML格式 context.Response.Write("<?xml version='1.0'?>"); context.Response.Write("<Info>"); context.Response.Write("<Name>"+data.Name+"</Name>"); context.Response.Write("<Sex>"+data.Sex.ToString()+"</Sex>"); context.Response.Write("<Nation>"+data.Nation+"</Nation>"); context.Response.Write("<Birthday>"+data.Birthday.Value.ToString("yyyy年MM月dd日")+"</Birthday>"); context.Response.Write("<aa><Name>hello</Name></aa>"); context.Response.Write("</Info>"); context.Response.End(); } public bool IsReusable { get { return false; } } }