关于Ajax的简单例子

引用自:http://www.cnblogs.com/singlepine/archive/2006/01/06/253393.html
Ajax 是 “Asynchronous JavaScript + XML”的简称,也就是异步的JavaScript和XML处理。从原理上看,主要是Ajax可以通过调用HttpRequest实现与服务器的异步通讯,并最终在网页中实现丰富友好的用户界面
Ajax使用初步,配置步骤
1.把Ajax.dll copy到应用系统bin目录下,然后在工程引用中引用Ajax.dll,如果没有的话可以下载/Files/singlepine/Ajax.rar
2.配置web.config,添加如下信息

<system.web>
        
<httpHandlers>
            
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
        
</httpHandlers>
3. aspx页面文件 WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>

<!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>无标题页</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" type="text/javascript">
  
function testAjax()
  
{
   
var first=document.getElementById("txtfirst");
   WebForm1.GetNIVNumber(first.value,callback_GetNIVNumber);
  }

  
function callback_GetNIVNumber(res)
  
{
   
var obj=document.getElementById("txtsecond");   
   obj.value
=res.value;    
  }

  
</script>
 
</head>
 
<body >
  
<form id="Form1" method="post" runat="server">
   
<input type="button" value="Button"  onclick="testAjax();" id="Button1"/>
      
<br />
   
<asp:TextBox id="txtfirst"  runat="server"></asp:TextBox><br />
   
<asp:TextBox id="txtsecond" runat="server"></asp:TextBox>
  
</form>
 
</body>
</html>

3. 类文件。
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;

namespace WebApplication7
{
    
public partial class WebForm1 : System.Web.UI.Page
    
{
        
protected void Page_Load(object sender, EventArgs e)
        
{
            Ajax.Utility.RegisterTypeForAjax(typeof(WebApplication7.WebForm1));
          }

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        
public static string GetNIVNumber(string str)
        
{
            
return str;
        }

    }

}

4. 演示结果,点铵钮后将第二个框中显示第一个框中的值。 
posted @ 2007-12-04 14:17  wj-conquer  阅读(301)  评论(0编辑  收藏  举报