<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SumNumber.aspx.cs" Inherits="SumNumber" %>
<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 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("sum").value=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<br />無重新整理加法運算簡單樣本<br />
<br />
<input id="num1" style="width: 107px" type="text" onkeyup="AddNumber();" value="0" />
+<input id="num2" style="width: 95px" type="text" onkeyup="AddNumber();" value="0" />
=<input id="sum" style="width: 97px" type="text" /></div>
</form>
</body>
</html>
這個執行個體中還用到了程式處理檔案,代碼如下:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
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;
}
}
}