热点推荐:ASP.Net | ADO.Net | VB.Net | Web服务器 | Access | MSSQL | MySQL | Oracle | .Net控件 | Win 9x | Win 2000 | Win 2003 | DOS | Unix | 注册表 | 应用其它 | 安装调试 | 基本操作 | 使用技巧 | 系统优化 |故障处理 | 个性风格 | 病毒安全 | 专杀工具
您现在的位置: 中华IT技术网 >> 开发语言 >> Ajax >> 正文
全文
QuickGuide for AJAX[简译AJAX快速指南]以及对现有WebService的扩展
作者:1024k    文章来源:本站原创    点击数:    更新时间:2007-9-22

AJAX .Net Wrapper quick usage guide

(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] [2] 下一页

  • 上一篇文章:
  • 下一篇文章: 没有了
  • 相关文章
    最新更新
    编辑推荐
    热门图片
    频道大全
    文章阅读排行
    周排行
    月排行