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('現在服務器上的時間是:/n' + 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"); } public string RaiseCallbackEvent(string eventArgument) { return DateTime.Now.ToString(); } } 運行,點按鈕結果如下: 第二種方法:在上面的方法中我們必須要在前臺綁定後臺,那麼如果不綁定呢?我們這樣做: 直接把GetCallbackEventReference當做js函數中的一個實現內容,然後把這個js函數註冊到客戶端。 前臺TestPage代碼: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %> <html> <head> <title>Untitled Page</title> <script type="text/javascript"> function test() { var lb = document.getElementById("Select1"); //取的那個下拉框 var con = lb.options[lb.selectedIndex].text; //得到你選擇的下拉框的文本再調用呢個CallTheServer,是一個由服務器端輸出的js函數 CallTheServer(con,''); } function ReceiveServerData(rValue) { Results.innerHTML = rValue; } </script> </head> <body> <form id="form1" runat="server"> <div> <select id="Select1"> <option value=1 selected="selected">老鼠徒弟</option> <option value=2>吳旗娃師傅</option> </select> <br /> <br /> <input onclick="test()" value="從服務器返回下拉框文本" type=button> <br /> <br /> <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 TestPage : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler { protected void Page_Load(object sender, EventArgs e) { String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); String callbackScript; callbackScript = "function CallTheServer(arg,context)" + "{ " + cbReference + "} ;"; Page.ClientScript.RegisterStartupScript(this.GetType(),"abcdefg",callbackScript, true); //第四個參數代表是不是要自動給着腳本加上<script type="text/javascript"></script>標記,當然要加啊 } public String RaiseCallbackEvent(String eventArgument) { return "你選擇的是" + eventArgument; } } 下面是執行結果: 第三種:前面兩種都是<input type="button"的html控件,那麼如果是服務器按鈕呢?當然也可以,在後臺添加服務器按鈕的onclick 屬性。 前臺third.aspx代碼: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="third.aspx.cs" Inherits="third" %> <html> <head> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <select id="Select1"> <option selected="selected" value=1>老鼠徒弟</option> <option value=2>吳旗娃師傅</option> </select> <asp:Button ID="Button1" runat="server" Text="這是個服務器按鈕" /></div> <div id="div1" /> <script type="text/javascript"> function Re(ret) { document.getElementById("div1").innerHTML = ret; alert(ret); } </script> </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 third : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler { protected void Page_Load(object sender, EventArgs e) { //第四個參數爲null,因爲你不可能再在js中給他傳參數了 string str = Page.ClientScript.GetCallbackEventReference(this,"document.getElementById('Select1').options[document.getElementById('Select1').selectedIndex].text","Re",null); //return false是爲了防止提交窗體 Button1.Attributes.Add("onclick",str+";return false;"); } #region ICallbackEventHandler Members public string RaiseCallbackEvent(string eventArgument) { if (eventArgument == "老鼠徒弟") { return "老鼠徒弟:人生如鼠,不在倉就在廁!"; } else { return "吳旗娃師傅:自信自強,樂觀向上"; } } #endregion } 小技巧,當你寫完System.Web.UI.ICallbackEventHandler後,把鼠標移上去,那麼System前面會有個小圖表,點他會自動寫好那個RaiseCallbackEvent代碼,效果如下; |
Asp.Net2.0無刷新客戶端回調
Asp.Net2.0無刷新客戶端回調
2007-08-26 10:15
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.