`

在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页源代码:

<!-- <br /> <br />Code highlighting produced by Actipro CodeHighlighter (freeware) <br />http://www.CodeHighlighter.com/ <br /> <br />--><%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallbackDemo.aspx.cs" Inherits="CallbackDemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>CallbackDemo</title>

    
<script type="text/javascript">
        function GetServerDateTime(context)
...{
            
<%= ClientScript.GetCallbackEventReference(this, "", "ReceiveServerData", "")%>;
        }


        function ReceiveServerData(rValue)
...{
            document.getElementById(
"ResultsSpan").innerHTML = rValue;

        }

    
</script>

</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<input id="btnSubmit" type="button" value="现在时间是" onclick="GetServerDateTime(ResultsSpan)" />
        
<br />
        
<span id="ResultsSpan" runat="server"></span>
    
</div>
    
</form>
</body>
</html>
 

 .aspx.cs源代码:

<!-- <br /> <br />Code highlighting produced by Actipro CodeHighlighter (freeware) <br />http://www.CodeHighlighter.com/ <br /> <br />-->using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CallbackDemo : System.Web.UI.Page,ICallbackEventHandler
...{
    
protected void Page_Load(object sender, EventArgs e)
    
...{

    }

    
public void RaiseCallbackEvent(String eventArgument)
    
...{
        
    }

    
public String GetCallbackResult()
    
...{
        
return DateTime.Now.ToString();
    }


}
 

    演示中客户端注册的脚本函数:<%= ClientScript.GetCallbackEventReference(this, "", "ReceiveServerData", "")%>;。这句动态生成一个客户端函数,该函数包含对来自GetCallbackEventReference方法的返回值的调用。ClientScriptManager.GetCallbackEventReference方法有4个重载函数,上面这个方法中,"this"是处理客户端回调的服务器控件,它必须实现ICallbackEventHandler 接口并提供RaiseCallbackEvent 方法,它的类型是System.Web.UI.Control。第二个参数是从客户端脚本传递给服务器的一个参数,它是String类型的。第三个函数是客户端事件处理程序的名称,它接收成功的服务器事件的结果,这里把回调结果返回给JS中的ReceiveServerData()方法。最后一个参数是启动回调之前在客户端计算的客户端脚本。脚本的结果传回客户端事件处理程序。

    JS方法:

<!-- <br /> <br />Code highlighting produced by Actipro CodeHighlighter (freeware) <br />http://www.CodeHighlighter.com/ <br /> <br />-->function ReceiveServerData(rValue) ...{
    document.getElementById(
"ResultsSpan").innerHTML = rValue;
}
 

 

接收服务器事件的结果,并将结果呈现出来。

    而服务器端的RaiseCallbackEvent()方法没有内容,因为这里是最简单的演示,我们没有回调参数,如果有回调参数在这个方法中处理。GetCallbackResult()则是返回当前的系统时间。

    在VS中测试刚才的DEMO,发现会在浏览器源代码中自动生成一段这样的代码:

<!-- <br /> <br />Code highlighting produced by Actipro CodeHighlighter (freeware) <br />http://www.CodeHighlighter.com/ <br /> <br />--><script src="/Callback/WebResource.axd?d=EsUKgUC2lEyf3iKipjXhYw2&amp;t=633655516704843750" type="text/javascript"></script>
 

 

这个DEMO产生的WebResource.axd有21KB大小,测试了一下,所有的文件都是一样的体积。它是在这个文件中创建了AJAX请求:

<!-- <br /> <br />Code highlighting produced by Actipro CodeHighlighter (freeware) <br />http://www.CodeHighlighter.com/ <br /> <br />-->
function WebForm_DoCallback(eventTarget, eventArgument, eventCallback, context, errorCallback, useAsync) ...{
    
var postData = __theFormPostData +
                
"__CALLBACKID=" + WebForm_EncodeCallback(eventTarget) +
                
"&__CALLBACKPARAM=" + WebForm_EncodeCallback(eventArgument);
    
if (theForm["__EVENTVALIDATION"]) ...{
        postData
+= "&__EVENTVALIDATION=" + WebForm_EncodeCallback(theForm["__EVENTVALIDATION"].value);
    }

    
var xmlRequest,e;
    
try ...{
        xmlRequest
= new XMLHttpRequest();
    }

    
catch(e) ...{
        
try ...{
            xmlRequest
= new ActiveXObject("Microsoft.XMLHTTP");
        }

        
catch(e) ...{
        }

    }

    
var setRequestHeaderMethodExists = true;
    
try ...{
        setRequestHeaderMethodExists
= (xmlRequest && xmlRequest.setRequestHeader);
    }

    
catch(e) ...{}
    
var callback = new Object();
    callback.eventCallback
= eventCallback;
    callback.context
= context;
    callback.errorCallback
= errorCallback;
    callback.async
= useAsync;
    
var callbackIndex = WebForm_FillFirstAvailableSlot(__pendingCallbacks, callback);
    
if (!useAsync) ...{
        
if (__synchronousCallBackIndex != -1) ...{
            __pendingCallbacks[__synchronousCallBackIndex]
= null;
        }

        __synchronousCallBackIndex
= callbackIndex;
    }

    
if (setRequestHeaderMethodExists) ...{
        xmlRequest.onreadystatechange
= WebForm_CallbackComplete;
        callback.xmlRequest
= xmlRequest;
        xmlRequest.open(
"POST", theForm.action, true);
        xmlRequest.setRequestHeader(
"Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        xmlRequest.send(postData);
        
return;
    }

    callback.xmlRequest
= new Object();
    
var callbackFrameID = "__CALLBACKFRAME" + callbackIndex;
    
var xmlRequestFrame = document.frames[callbackFrameID];
    
if (!xmlRequestFrame) ...{
        xmlRequestFrame
= document.createElement("IFRAME");
        xmlRequestFrame.width
= "1";
        xmlRequestFrame.height
= "1";
        xmlRequestFrame.frameBorder
= "0";
        xmlRequestFrame.id
= callbackFrameID;
        xmlRequestFrame.name
= callbackFrameID;
        xmlRequestFrame.style.position
= "absolute";
        xmlRequestFrame.style.top
= "-100px"
        xmlRequestFrame.style.left
= "-100px";
        
try ...{
            
if (callBackFrameUrl) ...{
                xmlRequestFrame.src
= callBackFrameUrl;
            }

        }

        
catch(e) ...{}
        document.body.appendChild(xmlRequestFrame);
    }

    
var interval = window.setInterval(function() ...{
        xmlRequestFrame
= document.frames[callbackFrameID];
        
if (xmlRequestFrame && xmlRequestFrame.document) ...{
            window.clearInterval(interval);
            xmlRequestFrame.document.write(
"");
            xmlRequestFrame.document.close();
            xmlRequestFrame.document.write(
'<html><body><form method="post"><input type="hidden" name="__CALLBACKLOADSCRIPT" value="t"></form></body></html>');
            xmlRequestFrame.document.close();
            xmlRequestFrame.document.forms[
0].action = theForm.action;
            
var count = __theFormPostCollection.length;
            
var element;
            
for (var i = 0; i < count; i++) ...{
                element
= __theFormPostCollection[i];
                
if (element) ...{
                    
var fieldElement = xmlRequestFrame.document.createElement("INPUT");
                    fieldElement.type
= "hidden";
                    fieldElement.name
= element.name;
                    fieldElement.value
= element.value;
                    xmlRequestFrame.document.forms[
0].appendChild(fieldElement);
                }

            }

            
var callbackIdFieldElement = xmlRequestFrame.document.createElement("INPUT");
            callbackIdFieldElement.type
= "hidden";
            callbackIdFieldElement.name
= "__CALLBACKID";
            callbackIdFieldElement.value
= eventTarget;
            xmlRequestFrame.document.forms[
0].appendChild(callbackIdFieldElement);
            
var callbackParamFieldElement = xmlRequestFrame.document.createElement("INPUT");
            callbackParamFieldElement.type
= "hidden";
            callbackParamFieldElement.name
= "__CALLBACKPARAM";
            callbackParamFieldElement.value
= eventArgument;
            xmlRequestFrame.document.forms[
0].appendChild(callbackParamFieldElement);
            
if (theForm["__EVENTVALIDATION"]) ...{
                </span
分享到:
评论

相关推荐

    一个基于ajax+ASP.NET实现的客户端Callback 控件源码例子

    一个基于ajax+ASP.NET实现的客户端Callback 控件源码例子。

    ASP.NET Ajax 文件上传进度条源码示例

    随着ajax技术的流行以及用户体验得到越来越高的重视,各种注视用户体验的程序出现,比如带进度条的文件上传,看了网上很多资料还没找到真正意义上的ASP.NET实现进度条上传.Ajax 文件上传进度条,ASP.NET 文件上传...

    asp.net知识库

    在ASP.NET中使用WINDOWS验证方式连接SQL SERVER数据库 改进ADO.Net数据库访问方式 ASP.NET 2.0 绑定高级技巧 简单实用的DataSet更新数据库的类+总结 [ADO.NET]由数据库触发器引发的问题 为ASP.NET封装的SQL数据库...

    wBox asp.net demo 秒杀thickbox ajax

    ——轻量级的弹出窗口jQuery插件,压缩后仅仅3.65Kb,基于jQuery1.4.2开发,主要实现弹出框的效果,并且加入了很多有趣的功能,比如callback函数,显示隐藏层,Ajax页面,iframe嵌入页面……

    Ajax for asp.net

    在web.config中设置httpHandleer标签 &lt;configuration&gt;&lt;br&gt; &lt;system.web&gt;&lt;br&gt; &lt;httpHandlers&gt;&lt;br&gt; ,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory,Ajax" /&gt; &lt;/httpHandlers&gt;&lt;br&gt; &lt;/system.web&gt;&lt;br&gt;...

    Asp.Net无刷新分页( jquery.pagination.js)

    采用Jquery无刷新分页插件jquery.pagination.js 实现无刷新分页效果 页面内容: &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; ...

    一个基于ASP.NET+C#实现的TreeViewGrid控件源码及例子程序

    该控件集成了GridView 与TreeView控件的优点, 能进行多列Grid数据的树型展示,支持 databind, postback, callback, Microsoft Ajax, Event (Edit, Select, Expand, Populate, Collapse, Remove, Update, Cancel, etc...

    漂亮的ComponentArtWebUI web界面

    ComponentArt Web.UI专为ASP.NET框架设计,它包含16个支持AJAX技术的优质用户界面控件,并可应用在ASP.NET 1.0, ASP.NET 2.0, ASP.NET AJAX框架中。 2008年3月27日,专注于ASP.NET Web.UI及Charting控件开发的...

    Ajax完全自学手册(源代码).rar

    11.1.3 CallBack实现ASP.NET Ajax应用 11.1.4 使用Microsoft ASP.NET Ajax控件实现Ajax应用 第12章 12.1 使用ASP.NET Ajax控件实现的页面无刷新的简单实例 第13章 AjaxGridView Ajax化的GridView ...

    Jcrop_swfupload_asp.net实例

    的asp.net实例,简单好学,是本人项目中的实例,看了网上写的太麻烦,自己写的。 Jcrop是一个jQuery插件,它能为你的WEB应用程序快速简单地提供图片裁剪的功能。 特点: 对所有图片均unobtrusively(无侵入的,保持...

    Ajax完全自学手册(PPT)

    11.1.3 CallBack实现ASP.NET Ajax应用 11.1.4 使用Microsoft ASP.NET Ajax控件实现Ajax应用 第12章 12.1 使用ASP.NET Ajax控件实现的页面无刷新的简单实例 第13章 AjaxGridView Ajax化的GridView ...

    漂亮的ComponentArtWebUI及Demo源码(商业版带安装序列号)part2

    ComponentArt Web.UI专为ASP.NET框架设计,它包含16个支持AJAX技术的优质用户界面控件,并可应用在ASP.NET 1.0, ASP.NET 2.0, ASP.NET AJAX框架中。 &lt;br&gt;2008年3月27日,专注于ASP.NET Web.UI及Charting控件开发...

    漂亮的ComponentArtWebUI及Demo源码(商业版带安装序列号)part1

    ComponentArt Web.UI专为ASP.NET框架设计,它包含16个支持AJAX技术的优质用户界面控件,并可应用在ASP.NET 1.0, ASP.NET 2.0, ASP.NET AJAX框架中。 &lt;br&gt;2008年3月27日,专注于ASP.NET Web.UI及Charting控件开发...

    BitmapCutter(图片在线裁剪源码 - ASP.NET+jQuery+HttpHandler开发)

    Callback为服务器端图片处理类, 通过使用Cutter封装客户端AJAX提交的数据, 然后调用Helper中的方法来完成图片处理。 BitmapScissors是一个HttpHandler, 通过客户端返回的 'action' 来调用Callback中的方法。 ...

    .NET中实现客户端联动菜单 (无刷新)

    // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /**//// /// Required method for Designer support - do not modify /// the ...

    ComponentArtWebUI商业源码

    专为ASP.NET而设计:为三个先进且更强大的框架而设计:ASP.NET 1.0, ASP.NET 2.0和ASP.NET AJAX。 强大的客户端呈现技术:行业中最先进的Web用户界面技术。 ComponentArt Web.UI 包含以下用户界面控件 Calendar...

    Ajax完全自学手册PPT和源代码(ptt格式)

    11.1.3 CallBack实现ASP.NET Ajax应用 11.1.4 使用Microsoft ASP.NET Ajax控件实现Ajax应用 第12章 12.1 使用ASP.NET Ajax控件实现的页面无刷新的简单实例 第13章 AjaxGridView Ajax化的GridView AjaxChartRoom ...

    塞普森信息管理系统 2008 奥运旗舰版源码

    12 使用AJAX:使用AJAX实现Callback,去除了页面Form处存储的ViewState,减少了网页大小,加速了页面浏览速度;页面与服务器的交互不再使用PostBack,奥运旗舰版使用Callback方式与服务器交互;页面不再有垃圾内容,...

    Ajax.Dll各个版本无刷新组件

    2. 还有一种就是用微软专门为Ajax开发的控件包,叫作ASP.NET AJAX Control Toolkit,这个我没太研究,网上有相关资料,想知道的可以自己去查,里面有很多现成的控件可以实现AJAX。 以上三种是我收集到的一些AJAX的...

Global site tag (gtag.js) - Google Analytics