cetusz 发表于 2013-1-29 11:45:05

在ASP.NET中使用Callback实现AJAX

在许多时候,我们需要从客户端运行服务器代码而不执行回发,要实现这样的效果有非常多的方法,可以使用ASP.NET 中的 AJAX 功能,也可以使用象AjaxPro这样的第三方控件。这里我们要讨论的是使用Callback来实现AJAX。     要实现客户端回调的ASP.NET页的CS代码与创建ASP.NET页的过程类似,但也存在一些区别。该页的服务器代码必须:1、实现ICallbackEventHandler接口;2、提供RaiseCallbackEvent方法的实现;3、提供GetCallbackResult方法的实现。同时在客户端必须三个脚本函数:1、一个函数调用帮助器方法,该方法执行对服务器的实际请求;2、客户端回调函数,处理回调事件的服务器代码的结果调用并接收该结果;3、第三个函数是执行实际服务器请求的 Helper 函数。当在服务器代码中使用 GetCallbackEventReference方法生成对此函数的引用时,ASP.NET 将自动生成此函数。
    现在,我们从实例来分析Callback的实现方法。
.aspx页源代码:
<div style=""><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallbackDemo.aspx.cs" Inherits="CallbackDemo" %>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif<html xmlns="http://www.w3.org/1999/xhtml">
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif<head runat="server">
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif    <title>CallbackDemo</title>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif    <script type="text/javascript">
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/ExpandedBlockStart.gifhttp://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/ContractedBlock.gif        function GetServerDateTime(context) ...{
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/InBlock.gif            <%= ClientScript.GetCallbackEventReference(this, "", "ReceiveServerData", "")%>;
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/ExpandedBlockEnd.gif        }
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/ExpandedBlockStart.gifhttp://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/ContractedBlock.gif        function ReceiveServerData(rValue) ...{
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/InBlock.gif            document.getElementById("ResultsSpan").innerHTML = rValue;
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/InBlock.gif
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/ExpandedBlockEnd.gif        }
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif    </script>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif</head>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif<body>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif    <form id="form1" runat="server">
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif    <div>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif        <input id="btnSubmit" type="button" value="现在时间是" onclick="GetServerDateTime(ResultsSpan)" />
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif        <br />
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif        <span id="ResultsSpan" runat="server"></span>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif    </div>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif    </form>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif</body>
http://blog.moozi.net/editors/fckeditor/editor/plugins/mzplugins/images/None.gif</html>
页: [1]
查看完整版本: 在ASP.NET中使用Callback实现AJAX