net的机制实现客户端回调
原理:
--利用CallBackManager-首先客户端必须支持XmlHttp
1.客户端代码调用服务器端方法-->请求aspx页面-〉实例化页面,调用所用到的事件,直到Pre_Render事件为止
2执行服务器代码->解析服务器响应->客户端回调方法,接收服务器方法结果
-------------------服务器端处理
1==CallBackManager--必须知道客户端回调方法的名称,才能在调用服务器端方法,由CallBackManager解析响应时,确定在客户端调用
那个方法
2==为了在处理服务器端方法后指定调用那个客户端方法,可以使用
Page.ClientSideScriptManger.GetCallBackEventReference()方法,
--上面的方法还可以指定服务器异常的时候调用那个客户端方法
----------------------限制
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testAjaxCall.aspx.cs" Inherits="test_testAjaxCall" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script>
//--正常回 调用
function ClientCallback(result,context)
{
// alert(result);
// document.getElementById("ccc").innerHTML=result
////window.alert(context)--会产生 ddd
document.getElementById("ccc").value=result
}
//--错误
function ClientErrorCallback(error,context)
{
alert("ddd"+error);
}
//--调用服务器端函数
function cctv()
{
ValidateNumber("123","ddd")
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="ccc" />
<button onclick="cctv()" />
</div>
</form>
</body>
</html>
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class test_testAjaxCall : System.Web.UI.Page, ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
//检查客户端的浏览器是否支持客户端回调
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!Request.Browser.SupportsCallback)
{
throw new ApplicationException("您的机器不支持客户端回调
");
}
//获得回调事件引用,其实就是把客户端调用服务器的方法在这获得它的脚本形式
//--调用下面代码的到的字符串 WebForm_DoCallback('__Page',arg,ClientCallback,ctx,ClientErrorCallback,false)
string src=Page.ClientScript.GetCallbackEventReference(
this,//执行IcallbackEventHandler的控件
"arg",//要发送给RaiseCallbackEvent的值--传递的参数,也就是调用这个函数的函数给当前函数传递的参数
"ClientCallback",//客户端回调方法的名称
"ctx",//有客户端事件传送回客户端回调方法的数值--cotext的数据
"ClientErrorCallback",//客户端错误回调方法的名称。这个方法在服务器抛出异常时执行
false);//--参数不明(大概是同步或异步请求)
//下面的这个客户端方法就是调用上面src里面的脚本--注意这个arg就是传送给上面的arg的参数~~
//ctx--是传送给上面函数的ctx~~明白了~~
string mainSrc=@"function ValidateNumber(arg,ctx)
{"+src+";}";
//--注册上面生成的脚本
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),//一-种类型,使同一个键可以使用两次
"ValidateNumber",//--标识脚本块的唯一键,就是mainSrc个代码块是-- ValidateNumber的名称
mainSrc,//--要添加到客户机中的脚本块
true);//--允许方法插入<script>标记
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
//客户端回调限制必须调用这个方法
public void RaiseCallbackEvent(string eventargs)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
//调用这个方法返回结果
public string GetCallbackResult()
{
return DateTime.Now.ToString(); ;
}
}
Ok上面就是原理了+代码的简单应用咯
,简单说就是把上面的回调封装到了控件的单击事件中了,不过这个做的不够好有待改进~~不过我困了![](https://www.cnblogs.com/Emoticons/QQ/14.gif)
using System;
using System.Data;
using System.Configuration;
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/// <summary>
/// Summary description for Class1
/// </summary>
namespace CallbackTest
{
public class CallbackButton : HtmlButton, ICallbackEventHandler
{
public string CallBackArgument
{
get
{
string s = (string)ViewState["CallbackButton"];
if (s == null)
{
return string.Empty;
}
return s;
}
set
{
ViewState["CallbackButton"] = value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
protected override void RenderAttributes(HtmlTextWriter writer)
{
base.RenderAttributes(writer);
writer.WriteAttribute("onClick", Page.ClientScript.GetCallbackEventReference(
this,//执行IcallbackEventHandler的控件
this.CallBackArgument,//要发送给RaiseCallbackEvent的值--传递的参数,也就是调用这个函数的函数给当前函数传递的参数
"ButtonCallback",//客户端回调方法的名称
base.UniqueID,//有客户端事件传送回客户端回调方法的数值--cotext的数据
"ButtonErrorCallback",//客户端错误回调方法的名称。这个方法在服务器抛出异常时执行
false) + ";return false;");//--参数不明(大概是同步或,false异步调用)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
public void RaiseCallbackEvent(string eventargs)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
//调用这个方法返回结果
public string GetCallbackResult()
{
return DateTime.Now.ToString(); ;
}
}
}
转载地址:http://www.cnblogs.com/ajaxren/archive/2007/05/06/736745.aspx
--利用CallBackManager-首先客户端必须支持XmlHttp
1.客户端代码调用服务器端方法-->请求aspx页面-〉实例化页面,调用所用到的事件,直到Pre_Render事件为止
2执行服务器代码->解析服务器响应->客户端回调方法,接收服务器方法结果
-------------------服务器端处理
1==CallBackManager--必须知道客户端回调方法的名称,才能在调用服务器端方法,由CallBackManager解析响应时,确定在客户端调用
那个方法
2==为了在处理服务器端方法后指定调用那个客户端方法,可以使用
Page.ClientSideScriptManger.GetCallBackEventReference()方法,
--上面的方法还可以指定服务器异常的时候调用那个客户端方法
----------------------限制
不能指定调用服务器端的那个方法,客户端回调统统调用RaiseCallbackEvent方法--该方法属于IcallbackEventHandler
-----ICallbackEventHandler接口带有2个方法必须重写
//入口-----------eventargs客户端发入的函数
public void RaiseCallbackEvent(string eventargs)
{
}
//调用这个方法返回结果
public string GetCallbackResult()
{
return DateTime.Now.ToString(); ;
}
//--照规矩先看代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Ok上面就是原理了+代码的简单应用咯
,简单说就是把上面的回调封装到了控件的单击事件中了,不过这个做的不够好有待改进~~不过我困了
![](https://www.cnblogs.com/Emoticons/QQ/14.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
转载地址:http://www.cnblogs.com/ajaxren/archive/2007/05/06/736745.aspx