ASP.NET2.0實現頁面無重新整理CallBack_修正版 ASP.NET2.0實現頁面無重新整理CallBack_修正版[轉貼]
在看到眾多網站轉載的這篇文章時,發現調式時有錯誤,特發表了此修正版
後面的例子大家針對原版舉一反三。
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事件裡<%=那個public後台字串% >.做個小實驗代碼如下:
前台ServerTime.aspx:為了方便去掉好多沒用的html
<%@ page language="C#" CodeFile="ServerTime.aspx.cs" Inherits="ServerTime_aspx" %>
<html>
<head>
<title>Server Time</title>
<script language="javascript">
function GetServerTime()
...{
var message = '';
var context = '';
<%=sCallBackFunctionInvocation%>
}
function ShowServerTime(timeMessage, context) ...{
alert('現在伺服器上的時間是: ' + timeMessage);
}
</script>
</head>
<body>
<form id="MainForm" runat="server">
<input type="button" value="得到伺服器端時間" onclick="GetServerTime();" />
</form>
</body>
</html>
後台:
using System;
using System.Web.UI;
public partial class ServerTime_aspx : Page,ICallbackEventHandler
...{
//一定要實現ICallbackEventHandler借口
public string sCallBackFunctionInvocation;
void Page_Load(object sender, System.EventArgs e)
...{
sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context");
}
String returnValue;
string ICallbackEventHandler.GetCallbackResult()
...{
return returnValue;
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
...{
returnValue = DateTime.Now.ToString();
}
}
注意:
這裡定義了一個字串 String returnValue;
在GetCallbackResult 和 RaiseCallbackEvent 前都加上了 ICallbackEventHandler,好象是C#就要加上這個,VB不用。
一個 string GetCallbackResult 和 void RaiseCallbackEvent 搞定!
--------------------------------------
再給大家一個例子:
前台:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Exp2.aspx.cs" Inherits="Exp2" %>
<!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 onclick="LookUpStock()">Look Up Stock</button>
<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
}