學習Ajax做的一個非同步呼叫例子

來源:互聯網
上載者:User
      看了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)
        {

        }
    }
}

以上就是,可以測試一下哦。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.