轉(zhuǎn)帖|其它|編輯:郝浩|2010-09-21 11:47:17.000|閱讀 904 次
概述:本文主要解決ASP.NET 提供的CaLLBack回調(diào)技術(shù)實(shí)現(xiàn)頁(yè)面無(wú)刷新的問(wèn)題,希望對(duì)大家有幫助。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
NET 2.0提供的CallBack技術(shù)通常用在自定義的控件內(nèi),這樣可以實(shí)現(xiàn)某控件的自動(dòng)回調(diào)技術(shù),以下代碼在VS2005中經(jīng)過(guò)調(diào)試,可以直接運(yùn)行。
Default.aspx 文件代碼如下
<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="//www.w3.org/1999/xhtml" >
<head runat="server">
<title>CaLLBack回調(diào)技術(shù)實(shí)現(xiàn)頁(yè)面無(wú)刷新</title>
</head>
<script language=javascript>...
function filldata()
...{
var city=document.getElementById("TextBox1").value;
//調(diào)用方法GetCallbackEventReference,獲取服務(wù)器端返回的結(jié)果。語(yǔ)法如下:
//第一個(gè)參數(shù):表示處理客戶端回調(diào)的服務(wù)器控件。該控件必須實(shí)現(xiàn)ICallbackEventHandler接口
//本例中的this表示調(diào)用回調(diào)方法的控件就是本頁(yè)
//第二個(gè)參數(shù):從客戶端腳本傳遞給服務(wù)器端的一個(gè)參數(shù),應(yīng)用在RaiseCallbackEvent方法中
//第三個(gè)參數(shù):一個(gè)客戶端處理程序的名稱,該處理程序接收服務(wù)器端事件的返回結(jié)果
//第四個(gè)參數(shù):?jiǎn)?dòng)回調(diào)之前在客戶端計(jì)算的客戶端腳本,又稱腳本上下文。腳本的結(jié)果傳回客戶端事件處理程序
<%=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">
使用回調(diào)技術(shù)實(shí)現(xiàn)局部刷新</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">
選擇區(qū)域列表</td>
<td style="width: 375px">
<asp:DropDownList ID="DropDownList1" runat="server" Width="246px">
</asp:DropDownList></td>
</tr>
</table>
</div>
</form>
</body>
</html>
后臺(tái)代碼如下:
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() /**/////返回回調(diào)事件的執(zhí)行結(jié)果
...{
return _data;
}
public void RaiseCallbackEvent(string eventArgument) //執(zhí)行回調(diào)事件
...{
//eventArgument表示傳遞到事件處理程序的事件參數(shù),一般用于服務(wù)器與客戶端之間傳遞的數(shù)據(jù)參數(shù)
switch (eventArgument)
...{
case "北京":
_data = "朝陽(yáng),海淀,東城,西城";
break;
case "上海":
_data = "浦東,靜安,徐匯,虹口";
break;
}
}
#endregion
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載