现在的位置: 首页 > 综合 > 正文

一个简单的Ajax 程序

2013年10月22日 ⁄ 综合 ⁄ 共 2420字 ⁄ 字号 评论关闭

一个简单的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="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />

      </httpHandlers>

   <!--- Modified by wzhu at 2007-01-12 For Ajax Handlers  -->

    ...

  <system.web>

</configuration>

 

2.       创建一个Form, 名称:Ajax_Form1, 名称空间:Namespace AjaxTest

3.       创一个Class, 名称: AjaxMethod.vb

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, Ajax world."

        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>

        

程序效果图:

【上篇】
【下篇】

抱歉!评论已关闭.