- Rongsen.Com.Cn 版权所有 2008-2010 京ICP备08007000号 京公海网安备11010802026356号 朝阳网安编号:110105199号
- 北京黑客防线网安工作室-黑客防线网安服务器维护基地为您提供专业的
服务器维护
,企业网站维护
,网站维护
服务 - (建议采用1024×768分辨率,以达到最佳视觉效果) Powered by 黑客防线网安 ©2009-2010 www.rongsen.com.cn
作者:黑客防线网安Ajax教程基地 来源:黑客防线网安Ajax教程基地 浏览次数:0 |
先看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" "http://www.w3.org/TR/xhtml11/DTD/ xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtm 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的无刷新页面,而且实现起来十分简单。
http://pyw0818.cnblogs.com/archive/2006/05/30/413416.html
我要申请本站:N点 | 黑客防线官网 | |
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479 |