浏览器大全:是一个提供流行浏览器教程、在线学习分享的学习平台!

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—在脚本结束时增加 "<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下拉框选中的

相关软件

2345加速浏览器官方版

2345加速浏览器官方版 | 56.2MB

2345加速浏览器官方版

新一代2345加速浏览器采用Chromium和IE双内核,主打极速与安全特性。基于Chromium深度定制,引入网页智能预加载技术,访问网页更快速..

QQ浏览器官方正式版

QQ浏览器官方正式版 | 49.67MB

QQ浏览器官方正式版

QQ浏览器秉承TT浏览器1-4系列方便易用的特点,但技术架构不同,交互和视觉表现也重新设计,采用Chromium内核+IE双内核,让浏览快速稳定...

百度浏览器最新版下载

百度浏览器最新版下载 | 13.3MB

百度浏览器最新版下载

q百度浏览器,是一款简洁轻快、智能懂你的浏览器。依靠百度强大的搜索平台,在满足用户浏览网页的基础上,它整合百度体系业务优势,带给用户更方便的浏览方式功能...

UC浏览器官方正式版

UC浏览器官方正式版 | 44.2MB

UC浏览器官方正式版

UC浏览器(UC Browser)是UC Mobile Limited在2004年8月开发的一款软件,分uc手机浏览器和uc浏览器电脑版。UC浏览器是全球使用量最大的第三方手机浏览器...

猎豹浏览器2022最新版下载

猎豹浏览器2022下载 | 45MB

猎豹浏览器2022最新版下载

猎豹安全浏览器对Chrome的Webkit内核进行了超过100项的技术优化,访问网页速度更快。其具有首创的智能切换引擎,动态选择内核匹配不同网页...

360安全浏览器官方版下载

360安全浏览器下载 | 21.4MB

360安全浏览器官方版下载

360安全浏览器拥有全国最大的恶意网址库,采用恶意网址拦截技术,可自动拦截挂马、欺诈、网银仿冒等恶意网址。独创沙箱技术,在隔离模式即使访问****也不会感染...