客户端回调控件- -(纯属乱想出来的[不过可以用阿哈哈])

本文详细介绍了 ASP.NET AJAX 中客户端回调的基本原理及其使用方式。通过具体示例展示了如何利用 CallBackManager 实现客户端与服务器端的交互,并解释了 ICallbackEventHandler 接口的作用。

- -怎么说呢利用net的机制实现客户端回调
原理:
--利用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(); ;
}

//--照规矩先看代码
r_1111111111.gif

ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="testAjaxCall.aspx.cs" Inherits="test_testAjaxCall" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>Untitled Page</title>
ExpandedBlockStart.gifContractedBlock.gif    
<script>dot.gif
InBlock.gif    
//--正常回 调用
InBlock.gif
    function ClientCallback(result,context)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
//        alert(result);
InBlock.gif//
        document.getElementById("ccc").innerHTML=result
InBlock.gif//
//window.alert(context)--会产生 ddd
InBlock.gif
document.getElementById("ccc").value=result
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//--错误
InBlock.gif
    function ClientErrorCallback(error,context)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        alert(
"ddd"+error);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//--调用服务器端函数
InBlock.gif
    function cctv()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      ValidateNumber(
"123","ddd")
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    
</script>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif    
<div>
None.gif    
<input id="ccc" />
None.gif    
<button onclick="cctv()" />
None.gif    
None.gif    
</div>
None.gif    
</form>
None.gif
</body>
None.gif
</html>
None.gif
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
None.gif
public partial class test_testAjaxCall : System.Web.UI.Page, ICallbackEventHandler
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//检查客户端的浏览器是否支持客户端回调
InBlock.gif

InBlock.gif        
if (!Request.Browser.SupportsCallback)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new ApplicationException("您的机器不支持客户端回调dot.gif");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//获得回调事件引用,其实就是把客户端调用服务器的方法在这获得它的脚本形式
InBlock.gif        
//--调用下面代码的到的字符串  WebForm_DoCallback('__Page',arg,ClientCallback,ctx,ClientErrorCallback,false)
InBlock.gif
        string src=Page.ClientScript.GetCallbackEventReference(
InBlock.gif                                                               
this,//执行IcallbackEventHandler的控件
InBlock.gif
                                                               "arg",//要发送给RaiseCallbackEvent的值--传递的参数,也就是调用这个函数的函数给当前函数传递的参数
InBlock.gif
                                                               "ClientCallback",//客户端回调方法的名称
InBlock.gif
                                                               "ctx",//有客户端事件传送回客户端回调方法的数值--cotext的数据
InBlock.gif
                                                               "ClientErrorCallback",//客户端错误回调方法的名称。这个方法在服务器抛出异常时执行
InBlock.gif
                                                               false);//--参数不明(大概是同步或异步请求)
InBlock.gif       
//下面的这个客户端方法就是调用上面src里面的脚本--注意这个arg就是传送给上面的arg的参数~~
InBlock.gif        
//ctx--是传送给上面函数的ctx~~明白了~~
InBlock.gif
        string mainSrc=@"function ValidateNumber(arg,ctx)
InBlock.gif                     {
"+src+";}";
InBlock.gif        
//--注册上面生成的脚本
InBlock.gif
        Page.ClientScript.RegisterClientScriptBlock(
InBlock.gif            
this.GetType(),//一-种类型,使同一个键可以使用两次
InBlock.gif
            "ValidateNumber",//--标识脚本块的唯一键,就是mainSrc个代码块是-- ValidateNumber的名称
InBlock.gif
            mainSrc,//--要添加到客户机中的脚本块
InBlock.gif
            true);//--允许方法插入<script>标记
InBlock.gif

InBlock.gif           
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//客户端回调限制必须调用这个方法
InBlock.gif
    public void RaiseCallbackEvent(string eventargs)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//调用这个方法返回结果
InBlock.gif
    public string GetCallbackResult()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return DateTime.Now.ToString(); ;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


Ok上面就是原理了+代码的简单应用咯
,简单说就是把上面的回调封装到了控件的单击事件中了,不过这个做的不够好有待改进~~不过我困了14.gif

None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//// <summary>
InBlock.gif
/// Summary description for Class1
ExpandedBlockEnd.gif
/// </summary>

None.gifnamespace CallbackTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class CallbackButton : HtmlButton, ICallbackEventHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public string CallBackArgument
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string s = (string)ViewState["CallbackButton"];
InBlock.gif                
if (s == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return string.Empty;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return s;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"CallbackButton"= value;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected override void RenderAttributes(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.RenderAttributes(writer);
InBlock.gif            writer.WriteAttribute(
"onClick", Page.ClientScript.GetCallbackEventReference(
InBlock.gif                                                               
this,//执行IcallbackEventHandler的控件
InBlock.gif
                                                               this.CallBackArgument,//要发送给RaiseCallbackEvent的值--传递的参数,也就是调用这个函数的函数给当前函数传递的参数
InBlock.gif
                                                               "ButtonCallback",//客户端回调方法的名称
InBlock.gif
                                                               base.UniqueID,//有客户端事件传送回客户端回调方法的数值--cotext的数据
InBlock.gif
                                                               "ButtonErrorCallback",//客户端错误回调方法的名称。这个方法在服务器抛出异常时执行
InBlock.gif
                                                               false+ ";return false;");//--参数不明(大概是同步或,false异步调用)
InBlock.gif

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void RaiseCallbackEvent(string eventargs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//调用这个方法返回结果
InBlock.gif
        public string GetCallbackResult()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return DateTime.Now.ToString(); ;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/ajaxren/archive/2007/05/06/736745.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值