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

}
 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1106261

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章