ASP.NET2.0實現無重新整理用戶端回調

來源:互聯網
上載者:User
asp.net|用戶端|重新整理|無重新整理  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="得到伺服器端時間" />
</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 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代碼,效果如下;


  第三個感覺最差!



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.