一个简单的Ajax 程序
一个简单的Ajax 程序
目的:点击界面上的一个Button,使textbox1 里的内容无剧新的传到 texbox2 中
首先要在我们公References 中引入ajax.dll
操作步骤:
1. 打开web.config
添加入下内容:
<configuration>
<system.web>
<!--- Modified by wzhu at 2007-01-12 For Ajax Handlers -->
<httpHandlers>
<add verb="POST,GET" path="
</httpHandlers>
<!--- Modified by wzhu at 2007-01-12 For Ajax Handlers -->
...
<system.web>
</configuration>
2. 创建一个Form, 名称:Ajax_Form1, 名称空间:Namespace AjaxTest
Namespace AjaxTest
Public Class AjaxMethod
'<Summary> C# language</Summary>
'[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
'<Summary> C# language</Summary>
<Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)> _
Public Function XO() As String
Return "Hello,
End Function
<Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)> _
Public Function GetText(ByVal str As String)
Return str
End Function
End Class
End Namespace
4. 在Ajax_Form1 的Page_Load事件添加以下代码,以实ajax 的注册:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Ajax.Utility.RegisterTypeForAjax(GetType(AjaxMethod)) '//此处的AjaxMethod 是Ajax 方法所要实现的类的名称
'Ajax.Utility.RegisterTypeForAjax(GetType(Ajax_Form1))
End Sub
'<Ajax.AjaxMethod()> _
'Public Function GetMessageOfTheDay() As String
' Return "Experience is the mother of wisdom"
'End Function
5 Ajax_Form1 在表示的页面的实现部分 (最好将 javascript 段放在 ..</html> 的后面. )
<!-- Modified by wzhu -->
<script language="javascript">
function testAjax()
{
var first=document.getElementById("TextBox1");
AjaxMethod.GetText(first.value,callback_GetText);
}
function callback_GetText(res)
{
var obj=document.getElementById("TextBox2");
obj.value=res.value;
}
/*
AjaxMethods.XO(XO_CallBack);
function XO_CallBack(response)
{
alert(response.value);
}
Ajax_Form1.GetMessageOfTheDay(GetMessageOfTheDay_CallBack);
function GetMessageOfTheDay_CallBack(response)
{
alert(response.value);
}
AjaxMethod.XO(XO_CallBack);
function XO_CallBack(response)
{
alert(response.value);
}*/
</script>
程序效果图: