最新文章
·通过Visual C#.NET建...
·.net中一些所封装的类...
·XML、DataSet、DataGr...
·SharpDevelop带你进入...
·利用Visual C#处理数...
·转贴:DataGrid/DataL...
·多层结构来开发ASP.NE...
·IIS5 HTTP500内部错误...
·简介使用ASP.NET访问O...
·创建ASP.NET WEB自定...
·Creating and display...
·利用WSE 加密SOAP报文...
相关文章
·asp.net里怎么提交数...
·坚持学asp.net——(十...
·将具有特殊格式的文件...
·自己写的一个资料验证...
·在 ASP.NET 中执行 UR...
·在气泡式提示窗口中显...
·可以限制中文输入的文...
·C++中处理XML文件
·如何定制你自己的Data...
·.NET架构的核心开发...
·描述与注册,发布Web...
·用Asp.net实现基于XML...
推荐文章
·VB连接SQL数据库的模...
·VB.NET实现DirectSoun...
·MD5算法研究
·由http暗藏通道看网络...
·线程的实现
·把aspx文件编译成DLL...
·.Net平台Windows Form...
·用 C# 编写一个停放在...
·关于 WinForm程序的登...
·走迷宫C#版(二)
·常用ASP。NET技巧
·VB.NET里奇怪的数组赋...
  您现在的位置: 休闲居 >> 网络学院 >> 网络编程 >> .NET开发 >> 

Whidbey中客户端回调机制(二)
  人气: 【字体:大 中 小】
  发布时间:2006-02-15 21:36:03

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

≡ 查看、发表评论 ≡