学习Ajax做的一个异步调用例子

      看了OkAjax网站上的例子,学习了一下Ajax,自己仿照作了做了,果然挺好,下面他代码公布一下;
   首先,新建一页面,把一下html代码覆盖原html即可,.cs文件不用谢任何代码
 
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.1//EN "   " http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
    
< title > Untitled Page </ title >
    
< script language = " javascript "  type = " text/javascript " >
    var zipField 
=   null ;
function zipChanged(){
zipField 
=  document.getElementById( " zipcode " )
var zip 
=  zipField.value;
updateCity(zip);
}
function updateCity(zip) {
 var cityField 
=  document.getElementById( " city " );
 ask(
" Default2.aspx? lookupType=city&zip= " + zip,cityField,zipField);
}
</ script >
< script language = " javascript "  type = " text/javascript " >
HTTPRequest 
=  function () {
   var xmlhttp
= null ;
   
try  {
      xmlhttp 
=   new  ActiveXObject( " Msxml2.XMLHTTP " );
   } 
catch  (_e) {
      
try  {
         xmlhttp 
=   new  ActiveXObject( " Microsoft.XMLHTTP " );
       } 
catch  (_E) {    }
   }
   
if  ( ! xmlhttp  &&   typeof  XMLHttpRequest  !=   ' undefined ' ) {
     
try  {
        xmlhttp 
=   new  XMLHttpRequest();
     } 
catch  (e) {
        xmlhttp 
=   false ;
   }  }
   
return  xmlhttp;
}
function ask(url, fieldToFill, lookupField) 
{
      var http 
=   new  HTTPRequest();
         http.open(
" GET " , url,  true );
      http.onreadystatechange 
=  function ()
      { 
         handleHttpResponse(http, fieldToFill, lookupField)
      }
      http.send(
null );
}
function handleHttpResponse(http, fieldToFill, lookupField)
 {
  
if  (http.readyState  ==   4 )
  {
    result 
=  http.responseText;
       
if  ( - 1   !=  result.search( " null " )) 
       {
        lookupField.style.borderColor 
=   " red " ;
        fieldToFill.value 
=   "" ;
    } 
    
else  
    {
        lookupField.style.borderColor 
=   "" ;
        fieldToFill.value 
=  result;
    }
   }
}
    
</ script >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
        邮编:
< asp:TextBox ID = " zipcode "  runat = " server "  onKeyUp = " zipChanged() "   ></ asp:TextBox >
        城市:
< asp:TextBox ID = " city "  runat = " server " ></ asp:TextBox >& nbsp;
    
</ form >
</ body >
</ html >

 

   然后在新建一页面,起名叫Default2.aspx,然后,把里面除了<@Page>这一行不用删,其他的代码,全删掉
最后把此.cs文件,文件覆盖掉你的cs文件即可

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
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  Default2 : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
try
        {
            
string  sValue  =  Request.QueryString[ " zip " ].ToString();
            
switch  (sValue)
            {
                
case   " 0312 " :
                    Response.Write(
" 河北 " );
                    
break ;
                
case   " 010 " :
                    Response.Write(
" 北京 " );
                    
break ;

            }
        }
        
catch  (System.Exception e1)
        {

        }
    }
}

以上就是,可以测试一下哦!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值