欢迎光临爱时尚女性网
 

实现类似AJAX的无刷新页面

2012-6-24 编辑:knwz 来源:爱时尚女性网 阅读次数:934
  导读:先看MSDN Library 2005上的这个例子! ------ ClientCallback.aspx ------ Callback Test function ReceiveServerData(receivedStr, context) { alert(receivedStr); } ...
先看MSDN Library 2005上的这个例子!
------ ClientCallback.aspx ------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientCallback.aspx.cs" - Inherits="ClientCallback" %>







Callback Test








-------- 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接收服务器返回的数据,



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


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

public string GetCallbackResult()
{
return returnStr;
}

最后,将客户端方法和服务器端的方法关联起来,在Page_Load中实现,
ClientScriptManager cm = Page.ClientScript;
燬tring cbReference = cm.GetCallbackEventReference(this,- "arg", "ReceiveServerData", "");
String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
燾m.RegisterClientScriptBlock(this.- GetType(), "CallServer", callbackScript, true);

大功告成!

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

文章出自:爱时尚女性网www.aspjc.com,尊重版权是美德,转载请保留原地址,感谢合作!

 
 
链接交换请联系:QQ:790646582 首页链接要求百度快照在一周以内,不符合以上要求的各站,我们将定期把友情连接转入内页,谢谢合作。
Copyright @ 2012-2015 爱时尚女性网 保留所有权利吉ICP备14005127号-1 服务QQ:175529508 e-mail:zk8312@163.com
本站部分资源来自网友上传,如果无意之中侵犯了您的版权,请联系本站,本站将在3个工作日内删除。