站长学院通行证

 

返回“站长学院”网站首页

 当前位置 → 中国站长学院网络编程Ajax教程 → 浏览正文


利用ICallbackEventHandle实现类似AJAX的无刷新页面


作者:中国站长学院[收集整理]     来源:互联网转载     点击数:     更新时间:2008年03月17日

【字体: 】           【背景色 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白(默认色) 】    【收藏本文】   【打印


先看MSDN Library 2005上的这个例子!
------ ClientCallback.aspx ------
<%@ Page Language=C# AutoEventWireup=true CodeFile=ClientCallback.aspx.cs ­ Inherits=ClientCallback %>

<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.1//EN xhtml11.dtd>

<html xmlns= l >
    <head id=Head1 runat=server>
        <title>Callback Test</title>
        <script type=text/javascript>
        function ReceiveServerData(receivedStr, context)
        {
            alert(receivedStr);
        }
        </script>
    </head>
        <body>
          <form id=form1 runat=server>
            <input type=button value=Callback onclick=CallServer(argument, context)/><br />
          </form>
    </body>
</html>-------- ClientCallbacp.aspx.cs  -----------
// ClientCallback.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;­
using System.Web.UI.HtmlControls;

public partial class ClientCallback : System.Web.UI.Page, System.Web.UI.ICallbackEventHandle­ r
{

    void Page_Load(object sender, EventArgs e)
    {
        ClientscriptManager cm = Page.Clientscript;
        String cbReference = cm.GetCallbackEventReference(this,­ arg, ReceiveServerData, );
        String callbackscript = function CallServer(arg, context) { + cbReference + ; };
        cm.RegisterClientscriptBlock(this.­ GetType(), CallServer, callbackscript, true);
    }

    private string returnStr;
    //function called by client, executed on server
    public void RaiseCallbackEvent(String eventArgument)   
    {
        //do something with return argument
        returnStr = eventArgument.ToUpper();
        return;
    }
   
    //function that sends result?
    public string GetCallbackResult()
    {
        return returnStr;
    }
}
客户端用ReceiveServerData接收服务器返回的数据,
<script type=text/javascript>
        function ReceiveServerData(receivedStr, context)
        {
            alert(receivedStr);
        }
 </script>

使用CallServer(argument, context)传递数据到服务器。

客户端ok了,接下来是服务器端,
实现ICallbackEventHandle­ 接口,
private string returnStr;
 public void RaiseCallbackEvent(String eventArgument)   
    {
        returnStr = eventArgument.ToUpper();
        return;
    }
   
public string GetCallbackResult()
    {
        return returnStr;
    }

最后,将客户端方法和服务器端的方法关联起来,在Page_Load中实现,
ClientscriptManager cm = Page.Clientscript;
 String cbReference = cm.GetCallbackEventReference(this,­ arg, ReceiveServerData, );
String callbackscript = function CallServer(arg, context) { + cbReference + ; };
 cm.RegisterClientscriptBlock(this.­ GetType(), CallServer, callbackscript, true);

大功告成!

这种方法也能达到类似ajax的无刷新页面,而且实现起来十分简单。


责任编辑:中国站长学院

注册投稿

收藏本文到  >>>

相关文章

模板教程【Pagetitle 标记】04/11 
模板教程【Prenext 标记】(十04/11 
模板教程【Pagebreak 标记】04/11 
模板教程【Pagelist 标记】(04/11 
模板教程【List 标记】(十一)04/11 
模板教程【Channelartlis04/11 
模板教程【Mynews 标记】(九)04/11 
模板教程【Flink 标记】(八)04/11 
模板教程【Vote 标记】(七)04/11 
模板教程【Mytag 标记】(六)04/11 
模板教程【Type 标记】(四)04/11 
模板教程【Channel 标记】(三04/11 
模板教程【Field 标记】(二)04/11 
模板教程【Arclist 标记使用实04/11 
DedeCMS模板制作使用实例教程序04/11 
从零开始学DedeCms模板,模板制04/11 

推荐导读

热门资讯

资讯快报

论坛新贴

返回首页

关于我们 - 帮助(?) - 广告服务 - 诚征英才 - 友情链接 - 网站地图 - 站长BLOG

返回网站顶部

Copyright © 2007 中国站长学院(CNZZXY.COM). All Rights Reserved
站长学院服务资咨热线:0755-84289786    业务合作:(0)13554938953  (0)13423986856    网络宽带及主机提供:深圳傲网科技
粤ICP备08006968号   站长:忧郁的秋天(向管理员发邮件)   站长交流QQ群①:1815595   站长交流QQ群②:32960074