Whidbey中客戶端回調機制(二)

你將學會如何書寫實施的代碼,但是首先要處理怎樣處理客戶端回叫服務器並且處理服務器迴應的方法。 在期間你也需要保證CallBackManager 知道這種回叫客戶端方法。Listing 1 顯示了Page_Load 事件。

Listing 1

C# <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Listing 1: Registering Client Scripts:

This code snippet from the default page's

Page_Load event shows how you register client scripts.

if (!Page.IsPostBack)

{

// Get the callbackevent reference.

string bScript = Page.GetCallbackEventReference(this, "arg",  

   "CallBackHandler", "ctx", "ErrorCallBack");

StringBuilder sb = new StringBuilder();

 

// create the Javascript function that makes the 

// actual server call.

sb.Append("function CallServer(arg,ctx)/n{/n");

sb.Append(bScript);

sb.Append("/n}");

 

// Register the clientscript. 

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), 

   "CallServer", sb.ToString(), true);

// Add attributes for onchange events

cboRegion.Attributes.Add("onchange", "SelectRegion();");

cboCountry.Attributes.Add("onchange", "return SelectCountry();");

                

//Fetch the regiondata and bind it to cboRegion...

GetCallbackEventReference使得客戶端方法在客戶端請求結束時得到回收 它也讓CallBackManager 確定產生哪種回叫方法。 在這個例子內使用的被重載的方法是:

   public string GetCallbackEventReference(

      string target, string argument,

      string clientCallback, string  context, 

string clientErrorCallback) 

Table 1. GetCallBackEventReference 方法的參數描述。

Parameters

Description

target

ID of the page where the callback invocation is handled. For more see the other overloaded options available in the next immediate section.

In our sample "this" is the argument value, since the callback is handled in the same page.

argument

This is the parameter defintion used to send value to the server. This value is received by parameter "eventArgument" at the server end using the RaiseCallbackEvent event.

"arg" becomes the first parameter name in our sample. The value is passed through this argument from the client.

clientCallback

Method name of the callback that is invoked after successful server call.

"CallBackHandler" is the method name that handles the callback.  

context

A parameter that is associated with the "argument" from the client. It usually should be used to identify the context of the call. You will understand this better from the sample implementation.

In the sample "ctx" is just another parameter definition used. The value for this is passed from the client.

clientErrorCallback

Name of the method that is called from the CallBackManager in case of any errors.

從這個方法返回的string:

   

   __doCallback('__Page',arg,CallBackHandler,ctx, ErrorCallBack)

 

另一個重載方法是:

   public string GetCallbackEventReference(

      Control control, string argument,

      string clientCallback, string  context) 

   

   public string GetCallbackEventReference(

      Control control, string argument,

      string clientCallback,  string  context, 

string clientErrorCallback) 

 

在上面Listing 1顯示的兩種重載方法唯一不同是在使用Control參數方面。當你在一個控件內處理IcallbackEventHandler時,你便需要一個Control參數。 下面,Listing 1 顯示如下的代碼的部分:


  Page.ClientScript.RegisterClientScriptBlock(

      this.GetType(), "CallServer", sb.ToString(), true);

The call to RegisterClientScript renders the client script block in the page output. If you "view source" in the resulting client page you'll see the code shown below.

   function CallServer(arg,ctx)

   {

      __doCallback('__Page', arg, CallBackHandler, 

         ctx, ErrorCallBack);

   }

使客戶端發出回調

你可以使用這個函數在客戶端請求結束時通過處理有關的 arg ctx這兩個參數向服務器

發出回叫請求。然而,你也應該考慮在ASP.NET1 版本和2版本之間發生的

RegisterClientScriptBlock 定義方面的變化

ASP.NET v2.0,你可以使用ClientScriptManager在目標頁面註冊腳本塊。

Page.property ClientScript 返回這個ClientScriptManager 方法。 可以在所註冊的腳

本塊中選擇兩種重載的方法; 這是我爲這個例子選擇的:

 

 

public void RegisterClientScriptBlock(

      Type type, string key,                           

string script, bool addScriptTags)

 

2顯示了回叫服務器的參數

2:

Arguments

Description

Type

這個參數使得RegisterStartUpScript RegisterClientScriptBlock 可以這冊相同名字的腳本塊而不會衝突。

如:在例子中的This.GetType()

Key

腳本塊的標識.

如:"CallServer" .

Script

發出實際的腳本塊.

在例子中StringBuilder提供了這個值.

addScriptTags

一個bool

True—在腳本結束時增加 "<Script>" 標籤.

 

 

你最後要做的事情就是在page load事件執行期間綁定region下拉框,以便使用戶選擇, page load事件執行時只有region下拉框被綁定

下一步是通過被選擇的地區。 從處理客戶端到服務器返回的值, page load事件執行時已經爲下拉框增加 onchange屬性。如下:

   cboRegion.Attributes.Add("onchange", 

      "SelectRegion();");

   cboCountry.Attributes.Add("onchange", 

"SelectCountry();");

 

Listing 2 顯示了SelectRegion 功能的實現
 

JavaScript

Listing 2: When Regions Change:

The onchange event of the region dropdown list

runs this JavaScript SelectRegion method.

function SelectRegion()

{

//get the region dropdown(select) item.

var region = document.all.item("cboRegion") ;

//if selected value is valid.

  if(region.options[region.selectedIndex].value!=0)

  {

   //clear country box values

    var country = document.all.item("cboCountry") ;

    var countrylength = country.length;

    for(var countrycount=0;countrycount< } ,country.id); 
region.options[region.selectedIndex].value 
+ String.fromCharCode(20) 
CallServer(?Country? call. server the Make city.remove(city.options[0]);
 { ++) citylength;citycount citycount="0;citycount" 
for(var citylength="city.length;" var ; city values. box 
select clear country.remove(countrycount);>

Listing 2中的JavaScript函數清除在countrycity下拉框的數據( 如果有的話)。然後通過region the context參數調用 CallServer(Page_Load中定義)方法。 region參數值是Country + delimiter + region下拉框選中的值的組合。

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