ASP.NET2.0實(shí)現(xiàn)無刷新客戶端回調(diào)

2010-08-28 10:47:53來源:西部e網(wǎng)作者:

  Asp.Net2.0的客戶端回調(diào)是一種很讓人激動的方法,他能夠讓我們控制要提交什么數(shù)據(jù)給服務(wù)器而不用提交整個(gè)頁面,同時(shí)服務(wù)器也只返回你所需要的數(shù)據(jù)而不要發(fā)回整個(gè)頁面。

  首先我們要說一個(gè)很重要的方法:GetCallbackEventRefernce.我把我的理解寫出來,可能是錯誤的,懇請指出,非常感謝!

  GetCallbackEventReference首先實(shí)現(xiàn)讓客戶端腳本有能力傳遞參數(shù)給服務(wù)器端的RaiseCallbackEvent方法,然后返回RaiseCallBackEvent方法的值給你在GetCallbackEventRefernce方法中注冊的一個(gè)參數(shù)(其實(shí)也是一個(gè)你要在客戶端寫的腳本)。調(diào)用GetCallbackEventRefernce你必須從客戶端腳本中傳遞給他兩個(gè)參數(shù),一個(gè)是要傳遞給RaiseCallbackEvent事件的值,一個(gè)是context.

  他的參數(shù)意義如下:

  第一個(gè):實(shí)現(xiàn)了ICallbackEventHandler借口的頁面或者服務(wù)器控件,寫this代表但前頁面。

  第二個(gè):代表你從要從客戶端傳遞給服務(wù)器RaiseCallbackEvent方法的值

  第三個(gè):你要在客戶端寫的一個(gè)js函數(shù),同時(shí),服務(wù)器也會把計(jì)算得到的數(shù)據(jù)傳遞給這個(gè)函數(shù)做為這個(gè)函數(shù)的參數(shù)。

  第四個(gè):context具體什么意思我也不太清楚GetCallbackEventRefernce發(fā)送到了客戶、端的代碼是這樣的:

WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false)

  那么我們要怎么樣做才能夠從客戶端調(diào)用他呢?看到了三中方法:

  第一種:在后臺寫個(gè)public string,在Page_Load中給他賦值為:=Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context");注意在這里是Page.ClientScrip,因?yàn)樗麜祷貍(gè)ClientScriptManager,ClientScriptManager管理所有的客戶端腳本。然后在前臺某個(gè)按鈕的onclick事件里<%=那個(gè)public后臺字符串%>.做個(gè)小實(shí)驗(yàn)代碼如下:

  前臺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('現(xiàn)在服務(wù)器上的時(shí)間是:\n' + timeMessage);
}
</script>
</head>
<body>
<form id="MainForm" runat="server">
<input type="button" value="得到服務(wù)器端時(shí)間" onclick="GetServerTime();" />
</form>
</body>
</html>

  后臺:

using System;
using System.Web.UI;

public partial class ServerTime_aspx : Page,ICallbackEventHandler
{
 //一定要實(shí)現(xiàn)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();
 }
}


  運(yùn)行,點(diǎn)按鈕結(jié)果如下:

\

  第二種方法:在上面的方法中我們必須要在前臺綁定后臺,那么如果不綁定呢?我們這樣做:

  直接把GetCallbackEventReference當(dāng)做js函數(shù)中的一個(gè)實(shí)現(xiàn)內(nèi)容,然后把這個(gè)js函數(shù)注冊到客戶端。

  前臺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");
 //取的那個(gè)下拉框
 var con = lb.options[lb.selectedIndex].text;
 //得到你選擇的下拉框的文本再調(diào)用呢個(gè)CallTheServer,是一個(gè)由服務(wù)器端輸出的js函數(shù)
 CallTheServer(con,'');
}
function ReceiveServerData(rValue)
{
 Results.innerHTML = rValue;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="Select1">
<o(jì)ption value=1 selected="selected">老鼠徒弟</option>
<o(jì)ption value=2>吳旗娃師傅</option>
</select>
<br />
<br />
<input onclick="test()" value="從服務(wù)器返回下拉框文本" 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);
  //第四個(gè)參數(shù)代表是不是要自動給著腳本加上<script type="text/javascript"></script>標(biāo)記,當(dāng)然要加啊
 }
 public String RaiseCallbackEvent(String eventArgument)
 {
  return "你選擇的是" + eventArgument;
 }
}

  下面是執(zhí)行結(jié)果:

\


  第三種:前面兩種都是<input type="button"的html控件,那么如果是服務(wù)器按鈕呢?當(dāng)然也可以,在后臺添加服務(wù)器按鈕的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">
<o(jì)ption selected="selected" value=1>老鼠徒弟</option>
<o(jì)ption value=2>吳旗娃師傅</option>
</select>
<asp:Button ID="Button1" runat="server" Text="這是個(gè)服務(wù)器按鈕" /></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)
 {
  //第四個(gè)參數(shù)為null,因?yàn)槟悴豢赡茉僭趈s中給他傳參數(shù)了
  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 "吳旗娃師傅:自信自強(qiáng),樂觀向上";
  }
 }
 #endregion
}

  小技巧,當(dāng)你寫完System.Web.UI.ICallbackEventHandler后,把鼠標(biāo)移上去,那么System前面會有個(gè)小圖表,點(diǎn)他會自動寫好那個(gè)RaiseCallbackEvent代碼,效果如下;

\


  第三個(gè)感覺最差!

關(guān)鍵詞:ASP.NET