Ajax
HTML页面
<!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" language="javascript">
var XmlHttp;
function CreateXmlHttpRequest()
{
if(window.ActiveXObject)
{
try
{
XmlHttp=new ActiveXObject("MSXml2.XMLHTTP");
}
catch(e)
{ }
try
{
XmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{ }
}
else
{
XmlHttp=new XmlHttpRequest();
}
}
function SendAjax()
{
CreateXmlHttpRequest();
var url="Add.aspx?n1="+document.getElementById("t1").value+"&n2="+document.getElementById("t2").value;
XmlHttp.open("GET",url,true);
XmlHttp.onreadystatechange=getResponse;
XmlHttp.send(null);
}
function getResponse()
{
if(XmlHttp.readystate==4)
{
if(XmlHttp.status==200)
{
document.getElementById("t3").value=XmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<input id="t1" type="text" value="0" onkeyup="SendAjax()" />+
<input id="t2" type="text" value="0" onkeyup="SendAjax()" />=
<input id="t3" type="text" />
</body>
</html>
Add.aspx页面
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int n1 = Convert.ToInt32(Request.QueryString["n1"]);
int n2 = Convert.ToInt32(Request.QueryString["n2"]);
Response.Write(n1 + n2);
}
}