.NET 2.0提供的CallBack技术通常用在自定义的控件内,这样可以实现某控件的自动回调技术
以下代码在VS2005中经过调试,可以直接运行。
Default.aspx 文件代码如下
<%
...
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > CaLLBack回调技术实现页面无刷新 </ title >
</ head >
< script language =javascript > ...
function filldata()
...{
var city=document.getElementById("TextBox1").value;
//调用方法GetCallbackEventReference,获取服务器端返回的结果。语法如下:
//第一个参数:表示处理客户端回调的服务器控件。该控件必须实现ICallbackEventHandler接口
//本例中的this表示调用回调方法的控件就是本页
//第二个参数:从客户端脚本传递给服务器端的一个参数,应用在RaiseCallbackEvent方法中
//第三个参数:一个客户端处理程序的名称,该处理程序接收服务器端事件的返回结果
//第四个参数:启动回调之前在客户端计算的客户端脚本,又称脚本上下文。脚本的结果传回客户端事件处理程序
<%=Page.ClientScript.GetCallbackEventReference(this,"city","filldll",null) %>
}
function filldll(strcity)
...{
document.getElementById("DropDownList1").options.length=0;
var indexofcity;
var city;
while(strcity.length>0)
...{
indexofcity=strcity.indexOf(",");
if(indexofcity>0)
...{
city=strcity.substring(0,indexofcity);
strcity=strcity.substring(indexofcity+1);
document.getElementById("DropDownList1").add(new Option(city,city));
}
else
...{
document.getElementById("DropDownList1").add(new Option(strcity,strcity));
break;
}
};
}
</ script >
< body >
< form id ="form1" runat ="server" > < div >
< table >
< tr >
< td colspan ="2" >
使用回调技术实现局部刷新 </ td >
</ tr >
< tr >
< td style ="width: 217px; height: 21px" >
输入城市的名称 </ td >
< td style ="width: 375px; height: 21px" >
< asp:TextBox ID ="TextBox1" runat ="server" Width ="234px" ></ asp:TextBox ></ td >
</ tr >
< tr >
< td style ="width: 217px" >
</ td >
< td style ="width: 375px" >
< input id ="Button1" type ="button" value ="查 询" onclick ="filldata()" /></ td >
</ tr >
< tr >
< td style ="width: 217px" >
选择区域列表 </ td >
< td style ="width: 375px" >
< asp:DropDownList ID ="DropDownList1" runat ="server" Width ="246px" >
</ asp:DropDownList ></ td >
</ tr >
</ table >
</ div >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > CaLLBack回调技术实现页面无刷新 </ title >
</ head >
< script language =javascript > ...
function filldata()
...{
var city=document.getElementById("TextBox1").value;
//调用方法GetCallbackEventReference,获取服务器端返回的结果。语法如下:
//第一个参数:表示处理客户端回调的服务器控件。该控件必须实现ICallbackEventHandler接口
//本例中的this表示调用回调方法的控件就是本页
//第二个参数:从客户端脚本传递给服务器端的一个参数,应用在RaiseCallbackEvent方法中
//第三个参数:一个客户端处理程序的名称,该处理程序接收服务器端事件的返回结果
//第四个参数:启动回调之前在客户端计算的客户端脚本,又称脚本上下文。脚本的结果传回客户端事件处理程序
<%=Page.ClientScript.GetCallbackEventReference(this,"city","filldll",null) %>
}
function filldll(strcity)
...{
document.getElementById("DropDownList1").options.length=0;
var indexofcity;
var city;
while(strcity.length>0)
...{
indexofcity=strcity.indexOf(",");
if(indexofcity>0)
...{
city=strcity.substring(0,indexofcity);
strcity=strcity.substring(indexofcity+1);
document.getElementById("DropDownList1").add(new Option(city,city));
}
else
...{
document.getElementById("DropDownList1").add(new Option(strcity,strcity));
break;
}
};
}
</ script >
< body >
< form id ="form1" runat ="server" > < div >
< table >
< tr >
< td colspan ="2" >
使用回调技术实现局部刷新 </ td >
</ tr >
< tr >
< td style ="width: 217px; height: 21px" >
输入城市的名称 </ td >
< td style ="width: 375px; height: 21px" >
< asp:TextBox ID ="TextBox1" runat ="server" Width ="234px" ></ asp:TextBox ></ td >
</ tr >
< tr >
< td style ="width: 217px" >
</ td >
< td style ="width: 375px" >
< input id ="Button1" type ="button" value ="查 询" onclick ="filldata()" /></ td >
</ tr >
< tr >
< td style ="width: 217px" >
选择区域列表 </ td >
< td style ="width: 375px" >
< asp:DropDownList ID ="DropDownList1" runat ="server" Width ="246px" >
</ asp:DropDownList ></ td >
</ tr >
</ table >
</ div >
</ form >
</ body >
</ html >
后台代码如下:
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;
public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
... {
private string _data;
ICallbackEventHandler 成员#region ICallbackEventHandler 成员
public string GetCallbackResult() /**/////返回回调事件的执行结果
...{
return _data;
}
public void RaiseCallbackEvent(string eventArgument) //执行回调事件
...{
//eventArgument表示传递到事件处理程序的事件参数,一般用于服务器与客户端之间传递的数据参数
switch (eventArgument)
...{
case "北京":
_data = "朝阳,海淀,东城,西城";
break;
case "上海":
_data = "浦东,静安,徐汇,虹口";
break;
}
}
#endregion
}
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;
public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
... {
private string _data;
ICallbackEventHandler 成员#region ICallbackEventHandler 成员
public string GetCallbackResult() /**/////返回回调事件的执行结果
...{
return _data;
}
public void RaiseCallbackEvent(string eventArgument) //执行回调事件
...{
//eventArgument表示传递到事件处理程序的事件参数,一般用于服务器与客户端之间传递的数据参数
switch (eventArgument)
...{
case "北京":
_data = "朝阳,海淀,东城,西城";
break;
case "上海":
_data = "浦东,静安,徐汇,虹口";
break;
}
}
#endregion
}