ASP.NET无刷新客户端回调[多事件]

这是 ASP.NET无刷新客户端回调的多事件实现方法。

大概步骤思路如下:

1.客户端的事件会传给一串包含function参数的字符串给服务器端;

2.服务器端根据function的值,来执行相应的事件方法;

3.服务器端执行事件方法完毕,会回传一个带function参数的结果集给客户端;

4.客户端再根据function的值,进行相应的后续处理。

此功能与Ajax相比,显得非常鸡肋,适合于新手学习和研究。

前台代码:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div><input id="dog" type="text" />
     <a href="javascript:void(0)" onclick="Dog()">狗狗</a></div>
<div><input id="rabbit" type="text" />
     <a href="javascript:void(0)" onclick="Rabbit()">兔兔</a></div>
</form>
</body>
<script type="text/javascript">
function Dog(){
//str:客户端递交服务器端的参数串;
//function=Dog,服务器端获取参数串后,通过function的值判断应该执行哪个事件的方法
var
params=document.getElementById("dog").value;
//用&(其它符号)拼接参数串
var str="function=Dog&dog="+params;
//在此处调用服务端注册的函数
CallTheServer(str,'');
}
function Rabbit(){

var
params=document.getElementById("rabbit").value;
var str="function=Rabbit
&rabbit="+params;

CallTheServer(str,
'');
}
//回调函数,处理服务器返回的结果
function ReceiveServerData(rValue){
//所有的事件共用这个回调函数,所以我们需要解析rValue,得到function的值
//通过function的值判断是哪个事件返回的结果,然后执行该事件的方法
var str=new Array();
//str[0]:字符串"function=value"
//str[1]:返回的结果
str=rValue.split('&');

var function
=str[0].split('=')[1];
if(function=="Dog"){
alert(str[
1]);
}
if(function=="Rabbit"){
alert(str[
1]);
}
}
</script>
</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;
//ICallbackEventHandler接口,用于指示控件可以作为服务器的回调事件的目标
public class AspNetNoRefresh_AspNetNoRefresh : System.Web.UI.Page, ICallbackEventHandler
{
private string str = "";
protected void Page_Load(object sender, EventArgs e){
//获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器端事件的客户端回调
//参数(控件、参数、客户端脚本和上下文)
/*control 客户端回调的服务器控件。
argument 从客户端脚本传递给服务器端的一个参数
clientCallback 客户端回调函数,用来接收成功的服务器端事件的结果
context 启动回调之前在客户端计算的客户端脚本;脚本的结果传回客户端事件处理程序
*/
string cbReference = Page.ClientScript.GetCallbackEventReference(this,
"arg", "ReceiveServerData", "context");
//生成要注册的客户端调用函数CallTheServer
string callbackScript = "function CallTheServer(arg,context){" + cbReference + "};";
//利用RegisterStartupScript方法,注册CallTheServer
//参数(type、key、script和bool)
/*this.GetType() 获取当前实例类型
key        标识脚本块的唯一键
script 要发送到客户端的脚本内容
bool 指示是否添加脚本标记,为true就启用自动添加
*/
Page.ClientScript.RegisterStartupScript(
this.GetType(), "", callbackScript, true);
/*注册后实际发送到客户端的代码为:
WebForm_InitCallback();function CallTheServer(param,context)
{WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false)};
*/
}
// ICallbackEventHandler接口要实现的方法,处理以控件为目标的回调事件
//eventArgument:从客户端事件传递给该事件处理程序的参数串
public void RaiseCallbackEvent(string eventArgument){
string function = GetParams("function",eventArgument);
//根据function的值,执行相应的事件方法
if (function == "Dog"){
string dog = GetParams("dog",eventArgument);
//str:返回给客户端的结果
//客户端也需要根据function的值,执行相应的处理方法
str += "function=Dog&";
str
+= "这是小狗狗:" + dog;
}
if (function == "Rabbit"){
string rabbit = GetParams("rabbit", eventArgument);
str += "function=Rabbit&";
str
+= "这是可爱小兔兔:" + rabbit + ";我最爱你了!";
}
}
//ICallbackEventHandler接口要实现的方法,返回以控件为目标的回调事件的结果
// str:返回给客户端的结果
public string GetCallbackResult(){
return str;
}
// 解析参数串
//str:检查argument是否存在str,存在的话,取出str的值
// argument:待处理的参数串
protected string GetParams(string str, string argument){
string var ="";
if (argument.ToString().Trim() != ""){
string[] arr = argument.Split('&');//利用'&'分割字符串为字符串数组
for (int i = 0; i < arr.Length; i++){
if (arr[i].Contains(str)){//检查argument是否存在str,存在的话,取出=两边的值
//判断=左边的参数是否等于str,等于的话,把=号右的参数值赋给var
if (arr[i].Split('=')[0].ToString().Trim() == str){
var
= arr[i].Split('=')[1];
}}}
return var;
}
posted on 2010-08-04 17:26  林枫山  阅读(1015)  评论(0编辑  收藏  举报