Ajax
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %> <!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> AJAX 之加法运算示例 </title> <script type="text/javascript"> var xmlHttp; function createXMLHttpRequest() { if (window.ActivateXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } function addNumber() { createXMLHttpRequest(); var url = "Handler.ashx?Num1=" + document.getElementById("num1").value + "&Num2=" + document.getElementById("num2").value; xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange = showResult; xmlHttp.send(null); } function showResult() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { document.getElementById("result").value = xmlHttp.responseText; } } } </script> </head> <body> <form id="form1" runat="server"> <div style="text-align:center"> <input id="num1" style="width:99px" type="text" value="0" onkeyup="addNumber();"/> + <input id="num2" style="width:95px" type="text" value="0" onkeyup="addNumber();" /> = <input id="result" style="width:99px" type="text"/> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebApplication2 { /// <summary> /// Summary description for $codebehindclassname$ /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { //write your handler implementation here. context.Response.ContentType = "text/plain"; int result = Convert.ToInt32(context.Request.QueryString["Num1"]) + Convert.ToInt32(context.Request.QueryString["Num2"]); context.Response.Write(result); } public bool IsReusable { get { return false; } } } }
示例2:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!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 type="text/javascript"> var xmlHttp; function createXMLHttpRequest() { if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } function startRequest() { alert(1); createXMLHttpRequest(); // 待编 alert(xmlHttp); xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET","Response.xml", true); xmlHttp.send(null); } function handleStateChange() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { alert("来自服务器的响应:" + xmlHttp.responseText); } } } </script> </head> <body> <form id="form1" runat="server"> <div> <input id="Button1" type="button" value="发送请求" onclick= "startRequest();" /> <input id="ntx" type="text" name="nt" value="123"/> </div> </form> </body> </html>