Asp.Net2.0的用戶端回調是一種很讓人激動的方法,他能夠讓我們控制要送出什麼資料給伺服器而不用送出整個頁面,同時伺服器也隻傳回你所需要的資料而不要發回整個頁面。
首先我們要說一個很重要的方法:GetCallbackEventRefernce.
GetCallbackEventReference首先實作讓用戶端腳本有能力傳遞參數給伺服器端的RaiseCallbackEvent方法,然後傳回RaiseCallBackEvent方法的值給你在GetCallbackEventRefernce方法中注冊的一個參數(其實也是一個你要在用戶端寫的腳本)。調用GetCallbackEventRefernce你必須從用戶端腳本中傳遞給他兩個參數,一個是要傳遞給RaiseCallbackEvent事件的值,一個是context.
他的參數意義如下:
第一個:實作了ICallbackEventHandler借口的頁面或者伺服器控件,寫this代表但前頁面。
第二個:代表你從要從用戶端傳遞給伺服器RaiseCallbackEvent方法的值
第三個:你要在用戶端寫的一個js函數,同時,伺服器也會把計算得到的資料傳遞給這個函數做為這個函數的參數。
第四個:context具體什麼意思我也不太清楚GetCallbackEventRefernce發送到了客戶、端的代碼是這樣的:
WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false)
那麼我們要怎麼樣做才能夠從用戶端調用他呢?看到了三中方法:
第一種:在背景寫個public string,在Page_Load中給他指派為:=Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context");注意在這裡是Page.ClientScrip,因為他會傳回個ClientScriptManager,ClientScriptManager管理所有的用戶端腳本。然後在前台某個按鈕的onclick事件裡.做個小實驗代碼如下:
前台ServerTime.aspx:為了友善去掉好多沒用的html
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetServerTime()
{
CallBackResult("擦幹你的淚水");
}
function ShowServerTime(timeMessage, context) {
alert('現在伺服器上的時間是: ' + timeMessage);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="得到伺服器端時間" οnclick="GetServerTime();" />
<asp:Label ID="lbltime" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetServerTime()
{
CallBackResult("擦幹你的淚水");
}
function ShowServerTime(timeMessage, context) {
alert('現在伺服器上的時間是: ' + timeMessage);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="得到伺服器端時間" οnclick="GetServerTime();" />
<asp:Label ID="lbltime" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetServerTime()
{
CallBackResult("擦幹你的淚水");
}
function ShowServerTime(timeMessage, context) {
alert('現在伺服器上的時間是: ' + timeMessage);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="得到伺服器端時間" οnclick="GetServerTime();" />
<asp:Label ID="lbltime" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
背景:
public partial class AspAjax : System.Web.UI.Page, ICallbackEventHandler
{
public string sCallBackFunctionInvocation;
protected void Page_Load(object sender, EventArgs e)
{
sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "arg", "ShowServerTime", "context");
string ScriptFunction = "function CallBackResult(arg, context) { " + sCallBackFunctionInvocation + "} ;";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", ScriptFunction, true);
}
String returnValue;
public string GetCallbackResult()
{
return returnValue;
}
public void RaiseCallbackEvent(string eventArgument)
{
returnValue = eventArgument + DateTime.Now.ToString();
}
}
一個 string GetCallbackResult 和 void RaiseCallbackEvent 搞定!
--------------------------------------
再給大家一個例子:
前台:
DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<script type="text/javascript">
function LookUpStock()
...{
var lb = document.forms[0].ListBox1;
var product = lb.options[lb.selectedIndex].text
CallServer(product, "");
}
function ReceiveServerData(rValue)
...{
Results.innerText = rValue;
}
script>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" Runat="server">asp:ListBox>
<br />
<br />
<button οnclick="LookUpStock()">Look Up Stockbutton>
<br />
<br />
Items in stock: <span ID="Results">span>
<br />
div>
form>
body>
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;
public partial class Exp2 : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
...{
protected System.Collections.Specialized.ListDictionary catalog;
protected void Page_Load(object sender, EventArgs e)
...{
String cbReference =
Page.ClientScript.GetCallbackEventReference(this,
"arg", "ReceiveServerData", "context");
String callbackScript;
callbackScript = "function CallServer(arg, context)" +
"{ " + cbReference + "} ;";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
catalog = new System.Collections.Specialized.ListDictionary();
catalog.Add("monitor", 12);
catalog.Add("laptop", 10);
catalog.Add("keyboard", 23);
catalog.Add("mouse", 17);
ListBox1.DataSource = catalog;
ListBox1.DataTextField = "key";
ListBox1.DataBind();
}
string earg = "";
//#region ICallbackEventHandler 成員
public string GetCallbackResult()
...{
String returnValue;
if (catalog[earg] == null)
...{
returnValue = "-1";
}
else
...{
returnValue = catalog[earg].ToString();
}
return returnValue;
}
public void RaiseCallbackEvent(string eventArgument)
...{
//throw new Exception("The method or operation is not implemented.");
earg = eventArgument;
}
//#endregion
}