轉帖|其它|編輯:郝浩|2010-09-21 11:47:17.000|閱讀 904 次
概述:本文主要解決ASP.NET 提供的CaLLBack回調技術實現頁面無刷新的問題,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
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" "//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="//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
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載