Whidbey中客户端回调机制(二)

你将学会如何书写实施的代码,但是首先要处理怎样处理客户端回叫服务器并且处理服务器回应的方法。 在期间你也需要保证 CallBackManager 知道这种回叫客户端方法。 ** Listing 1 ** 显示了 ** Page_Load ** 事件。

Listing 1

** C# **


** 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.NET第1 版本和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—在脚本结束时增加 "
1<script></script>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus