Ajax编程起步
2006-01-30 16:24 和尚释然 阅读(367) 评论(0) 编辑 收藏 举报 Ajax定义为“Asynchronous JavaScript + XML”的简称,也就是异步的JavaScript和XML处理。从原理上看,主要是Ajax可以通过调用HttpRequest实现与服务器的异步通讯,并最终在网页中实现丰富友好的用户界面。
1.引用Ajax.dll
拷贝Ajax.dll文件到程序目录下的bin文件夹下,然后在工程中引用。Ajax.dll文件下载:/Files/guenli/Ajax.rar
2.配置web.config
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
3.然后在工程目录下新建一个文件AjaxMethod.cs,添加如下方法<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
//[Ajax.AjaxMethod]
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public static string GetNIVNumber(string str)
{
return str;
}
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public static string GetNIVNumber(string str)
{
return str;
}
4.在工程目录下建立页面AjaxForm.aspx
<HTML>
<HEAD>
<title>AjaxForm</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">
function testAjax(owner, item, evt)
{
var first=document.getElementById("txtfirst");
AjaxMethod.GetNIVNumber(first.value,callback_GetNIVNumber);
}
function callback_GetNIVNumber(res)
{
var obj=document.getElementById("txtsecond");
obj.value=res.value;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 40px" type="button" value="Button"
onclick="testAjax();">
<asp:TextBox id="txtfirst" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 40px" runat="server"></asp:TextBox>
<asp:TextBox id="txtsecond" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 72px" runat="server"></asp:TextBox>
</form>
</body>
</HTML>
<HEAD>
<title>AjaxForm</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">
function testAjax(owner, item, evt)
{
var first=document.getElementById("txtfirst");
AjaxMethod.GetNIVNumber(first.value,callback_GetNIVNumber);
}
function callback_GetNIVNumber(res)
{
var obj=document.getElementById("txtsecond");
obj.value=res.value;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 40px" type="button" value="Button"
onclick="testAjax();">
<asp:TextBox id="txtfirst" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 40px" runat="server"></asp:TextBox>
<asp:TextBox id="txtsecond" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 72px" runat="server"></asp:TextBox>
</form>
</body>
</HTML>
protected System.Web.UI.WebControls.TextBox txtfirst;
protected System.Web.UI.WebControls.TextBox txtsecond;
private void Page_Load(object sender, System.EventArgs e)
{
//注册,其中Document.Pages.AjaxTest.AjaxMethod为AjaxMethod.cs方法所在的命名空间
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
}
protected System.Web.UI.WebControls.TextBox txtsecond;
private void Page_Load(object sender, System.EventArgs e)
{
//注册,其中Document.Pages.AjaxTest.AjaxMethod为AjaxMethod.cs方法所在的命名空间
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
}
5.运行工程。
运行界面如下:
从上面的输入框传值到下面输入框。
6.源代码下载:
参考文章 小山的Ajax使用初步 。
作者:顾恩礼
出处:http://www.cnblogs.com/guenli/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。