日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

ASP.NET2.0实现无刷新客户端回调

發(fā)布時間:2025/5/22 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET2.0实现无刷新客户端回调 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Asp.Net2.0的客戶端回調(diào)是一種很讓人激動的方法,他能夠讓我們控制要提交什么數(shù)據(jù)給服務(wù)器而不用提交整個頁面,同時服務(wù)器也只返回你所需要的數(shù)據(jù)而不要發(fā)回整個頁面。

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

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

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

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

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

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

  第四個:context具體什么意思我也不太清楚GetCallbackEventRefernce發(fā)送到了客戶、端的代碼是這樣的: WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false)   那么我們要怎么樣做才能夠從客戶端調(diào)用他呢?看到了三中方法:

  第一種:在后臺寫個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('現(xiàn)在服務(wù)器上的時間是:\n' + timeMessage);
}
</script>
</head>
<body>
<form id="MainForm" runat="server">
<input type="button" value="得到服務(wù)器端時間" οnclick="GetServerTime();" />
</form>
</body>
</html>   后臺: using System;
using System.Web.UI;

public partial class ServerTime_aspx : Page,ICallbackEventHandler
{
  //一定要實現(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();
  }
}  運行,點按鈕結(jié)果如下:



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

  直接把GetCallbackEventReference當(dāng)做js函數(shù)中的一個實現(xiàn)內(nèi)容,然后把這個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");
  //取的那個下拉框
  var con = lb.options[lb.selectedIndex].text;
  //得到你選擇的下拉框的文本再調(diào)用呢個CallTheServer,是一個由服務(wù)器端輸出的js函數(shù)
  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 οnclick="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);
   //第四個參數(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">
<option selected="selected" value=1>老鼠徒弟</option>
<option value=2>吳旗娃師傅</option>
</select>
<asp:Button ID="Button1" runat="server" Text="這是個服務(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)
  {
   //第四個參數(shù)為null,因為你不可能再在js中給他傳參數(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 "吳旗娃師傅:自信自強,樂觀向上";
   }
  }
  #endregion
}

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

總結(jié)

以上是生活随笔為你收集整理的ASP.NET2.0实现无刷新客户端回调的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。