QuickGuide for AJAX[简译AJAX快速指南]以及对现有WebService的扩展。

AJAX .Net Wrapper quick usage guide
Karl Seguin - http://www.openmymind.net/ - copyright 2005

(view AjaxGuide.doc for more detailed information)


Step 1 -
   Create a reference to the ajax.dll file

Step 2 - Set up the HttpHandler
In the web.config, set up the HttpHandler, like:

<configuration>
  <system.web>
    <httpHandlers>
    <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
    </httpHandlers> 
    ...
  <system.web>
</configuration>


Step 3 -
In the Page_Load event, call the following function:

'vb.net
Public Class Index
  Inherits System.Web.UI.Page

  Private Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Ajax.Utility.RegisterTypeForAjax(GetType(Index))
  '...
  end sub
  '...
End Class


//C#
public class Index : System.Web.UI.Page{
   private void Page_Load(object sender, EventArgs e){
      Ajax.Utility.RegisterTypeForAjax(typeof(Index));     
      //... 
   }
   //... 
}

Step 4 -
In the codebehind of your page, add functions (like you normally would) that you'd like to be able to asynchrounsly called by client-side scripting.  Mark these functions with the Ajax.JavascriptMethodAttribute():

//C#
[Ajax.AjaxMethod()]
public int ServerSideAdd(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}

'VB.Net
<Ajax.AjaxMethod()> _
Public Function ServerSideAdd(ByVal firstNumber As Integer, ByVal secondNumber As Integer) As Integer
 Return firstNumber + secondNumber
End Function

The wrapper will automatically create a JavaScript function named "ServerSideAdd" which accepts to parameters.  When called, this server-side function will be called and the result returned.


Step 5 -
Using JavaScript, you can invote the ServerSideAdd function like you would any other JavaScript function.  You can call it using the two parameters, or optionally pass a call back function.  The name of the function is, by default, <name of class>.<name of server side function>, such as Index.ServerSideAdd:

alertIndex.ServerSideAdd(100,99));

OR

Index.ServerSideAdd(100,99, ServerSideAdd_CallBack);

function ServerSideAdd_CallBack(response){
 alert(response.value);
}

The response exposes three properties, error, value and request.


Note that you can return more complex objects that simple types.


See the demo for additional information:
http://ajax.schwarz-interactive.de/csharpsample/default.aspx

==========================================================

这是下载AJAX后里面的一个说明文件。简单粗略的翻译一下(没有按照原文翻译):
添加一个AJAX的方法操作如下:
第一步:在项目里添加对Ajax.DLL的引用。
第二步:修改Web.config文件:主要是添加一个Handle的引用。
<configuration>
  <system.web>
    <httpHandlers>
    <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
    </httpHandlers> 
    ...
  <system.web>
</configuration>
第三步:在页面载入时添加添下面的函数:
//C#
public class Index : System.Web.UI.Page{
   private void Page_Load(object sender, EventArgs e){
      Ajax.Utility.RegisterTypeForAjax(typeof(Index));     
      //... 
   }
   //... 
}
注:感觉这里“曲解”了WebService!特别是后面的函数。
第四步:在页面里添加Ajax的函数,让它具有属性:Ajax.JavascriptMethodAttribute():
//C#
[Ajax.AjaxMethod()]
public int ServerSideAdd(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
注:这里的函数是在后台的页面里添加,而不是独立的WebService!本质上讲,WebService与普通的页面一样,都是基于Http协议的,因此,一般页面也好,WebService的asmx页面也好,没有本质的区别,只是在给不同的程序引用的时候,一些协议不一样。但我个人觉得,Ajax的这做做法不是很可取,它把服务函数与页面函数都放在一起,管理上就会存在一些问题。而且,服务函数可能要给其它程序调用,而这里只能给自己页面里的JS来调用。
第五步:JS调用:

alert(Index.ServerSideAdd(100,99).value);//原文有误,原文为:alertIndex.ServerSideAdd(100,99));

OR

Index.ServerSideAdd(100,99, ServerSideAdd_CallBack);

function ServerSideAdd_CallBack(response){
 alert(response.value);
}

OK,完成了!

然而我却有以下几个问题不明白:
1、AJAX的函数是在页面里的,这样会不会使页面逻辑变的混乱,搞不清楚哪是服务函数,哪是页面函数。
2、JS的调用也只是在本页里,如果我们要跨页面调用函数,或者服务呢?(后来知道了,有类的引用,可以跨页面调用。)
3、可以直接调用已经存在的服务函数吗?如果项目里已经存在了大量的服务函数,难道我还一个一个的都改成页面函数吗?还要添加Ajax属性,我觉得不可取!最郁闷的是在页面转入时的Ajax注册,简直无法接受。想想吧,那样的话,你要改多少内容?

因此,我觉得我们所面临的问题,如果用JS直接调用WebService上的函数!而不是像Ajax那样,将页面函数修改后用JS来调用!对现有WebService的Ajax改造。

1、在用户控件或者页面里注册AJAX:
   Ajax.Utility.RegisterTypeForAjax(typeof(Webb.WAVE.WebService.Service4FolderAndFiles)); 
你可以注册任何的一个类,而并不局限于Page类,当然,你的函数也就可以写在Web Service里了。
2、修改原服务函数,添加Ajax属性:
  [Ajax.AjaxMethod()]
  [WebMethod]
  public int ServerSideAdd(int firstNumber, int secondNumber)
  {
   return firstNumber + secondNumber;
  }
即,你完全可以给一个函数两个属性,这样你的代码就不用修改的太多了。
3、JS调用,与上面的就没什么两样了。

总结:从上面的例子上看的出来,Ajax已经不在是局限于服务函数的调用,它将页面的后台函数前台化,可以在JS里方便的调用它们。当然,原有的Web Service函数也是都可以不改变的,这就为我们的项目改造提供的很好的方便,你愿意改就改,添加一个Ajax方法属性就行了。不想,重新再写也行。当然,里主要的还是原来的函数可以保证你的原有项目都能很好的运行。

最后提一下的就是注册Ajax类时候,Ajax.Utility.RegisterTypeForAjax(typeof(Webb.WAVE.WebService.Service4FolderAndFiles)); 
它向页面里添加了一些JS代码。正是这些代码请求了服务器上的函数,而服务器再通过Handle来处理不同的请求来定位到服务器上的函数。

原文地址:https://www.cnblogs.com/WuCountry/p/407956.html