日期:2013-11-17  浏览次数:20420 次

你将学会如何书写实施的代码,但是首先要处理怎样处理客户端回叫服务器并且处理服务器回应的方法。 在期间你也需要保证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,你可以使用ClientScriptMan