onkey 加法运算

来源:互联网 发布:淘宝一千零一夜哪里看 编辑:程序博客网 时间:2024/05/08 22:15

Addition:实现加法运算

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" 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>AJAX加法运算示例</title>

    <script type ="text/javascript" >

    var xmlHttp;

    function createXMLHttpRequest()

    {

        if(window.XMLhttpRequest)

        {

           xmlHttp= new XMLHttpRequest();//mozilla浏览器

        }

        else if(window.ActiveXObject)

        {

          try

          {

               xmlHttp=new ActiveXObject("Msxm12.XMLHTTP");//IE旧版本

          }

          catch(e)

          {}

          try

          {

           xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本

          }

          catch(e)

          {    

          }

          if(!xmlHttp)

          {

           window.alert("不能创建XMLHttpRequest对象实例");

           return false;

          }

        }

       

    }

    function addNumber()

    {

   

        createXMLHttpRequest();

        var url="Handler.ashx?Num1="+document.getElementById("TxtNum1").value+"&Num2="+document.getElementById("TxtNum2").value;

       ;

        xmlHttp.open("GET",url,true);

        xmlHttp.onreadystatechange=showResult;

        xmlHttp.send(null);

    }

    function showResult()

    {

        if(xmlHttp.readyState==4)

        {

           if(xmlHttp.status==200)//这里出错一次

           {

               document.getElementById("TxtResult").value=xmlHttp.responseText;

             

             

           }

        }

    }

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div style="text-align: center">

        <input id="TxtNum1" type="text" value="0" onkeyup="addNumber()" />+<input id="TxtNum2" type="text" value="0"  onkeyup ="addNumber()" />=

        <input id="TxtResult" type="text" /></div>

    </form>

</body>

</html>

 

 

Handler.ashx

<%@ WebHandler Language="C#" Class="Handler" %>

 

using System;

using System.Web;

 

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;

        }

    }

 

}

 
原创粉丝点击