先看MSDN Library 2005上的这个例子! <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/ xhtml11.dtd”> <html xmlns=”http://www.w3.org/1999/xhtm l” > public partial class ClientCallback : System.Web.UI.Page, System.Web.UI.ICallbackEventHandle r void Page_Load(object sender, EventArgs e) private string returnStr; 使用CallServer(argument, context)传递数据到服务器。 客户端ok了,接下来是服务器端, 最后,将客户端方法和服务器端的方法关联起来,在Page_Load中实现, 大功告成! 这种方法也能达到类似ajax的无刷新页面,而且实现起来十分简单。
—— ClientCallback.aspx ——
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”ClientCallback.aspx.cs” Inherits=”ClientCallback” %>
<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;
{
{
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);
}
//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>
实现ICallbackEventHandle 接口,
private string returnStr;
public void RaiseCallbackEvent(String eventArgument)
{
returnStr = eventArgument.ToUpper();
return;
}
public string GetCallbackResult()
{
return returnStr;
}
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);
利用icallbackeventhandle实现类似ajax的无刷新页面 _ajax教程
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用icallbackeventhandle实现类似ajax的无刷新页面 _ajax教程
相关推荐
-      Jquery处理Json字符串的实例
-      ASP+Ajax实现无刷新评论简单例子
-      AJAX的阻塞及跨域名解析
-      AJAX学习资料
-      [js]一个获取页面ip的正则
-      ajax用户注册代码
-      Ajax技术全解之一
-      用Asp+XmlHttp实现RssReader功能