现在的位置: 首页 > 综合 > 正文

XmlHttp实例

2014年07月21日 ⁄ 综合 ⁄ 共 2218字 ⁄ 字号 评论关闭
WebForm1.aspx:
-----------------------------------------------------------------------------------------------------------------------
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2003.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

<script language="javascript">
var xmlHttp;

function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}

function updateTotal()
{
frm=document.forms[0];
url="Default2.aspx?A=" + frm.elements['A'].value + "&B=" + frm.elements['B'].value;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=doUpdate;
xmlHttp.send();
return false;
}

function doUpdate()
{
if(xmlHttp.readyState==4)
{
document.forms[0].elements['TOT'].value=xmlHttp.responseText;
}
}
</script>

</HEAD>
<body MS_POSITIONING="GridLayout" onload="createXMLHttpRequest();">
<form>
<table height="72" cellspacing="0" cellpadding="0" width="256" border="0" style="WIDTH: 256px; HEIGHT: 72px">
<tr valign="top">
<td>first value:</td>
<td><input type="text" id="A" value="0" onkeyup="updateTotal();"></td>
</tr>
<tr valign="top">
<td>second value:</td>
<td><input type="text" id="B" value="0" onkeyup="updateTotal();"></td>
</tr>
<tr valign="top">
<td>Returned Total:</td>
<td><input type="text" id="TOT" value="0"></td>
</tr>
</table>
</form>
</body>
</HTML>
-------------------------------------------------------------------------------------------------------------------------

Default2.aspx:
----------------------------------------------------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
int a=0;
int b=0;
if(Request.QueryString["A"]!=null)
{
a=System.Convert.ToInt16(Request.QueryString["A"].ToString());
}
if(Request.QueryString["B"]!=null)
{
b=System.Convert.ToInt16(Request.QueryString["B"].ToString());
}
this.Response.Write(a+b);
}
----------------------------------------------------------------------------------------- 

抱歉!评论已关闭.