客户端你将学会如何书写实施的代码,但是首先要处理怎样处理客户端回叫服务器并且处理服务器回应的方法。 在期间你也需要保证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—在脚本结束时增加 "<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 listruns 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函数清除在country和city下拉框的数据( 如果有的话)。然后通过region和 the context参数调用 CallServer(在Page_Load中定义)方法。 region参数值是Country + delimiter + region下拉框选中的值的组合。
Whidbey中客户端回调机制(二)
80酷酷网 80kuku.com