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

第二个参数 (context) 是与country下拉框同步统一。(我将会稍后在这个文章中解释在去限器和那context参数的使用)。 如果你重新请求, 服务器端 RaiseCallbackEvent 仍然执行。在Listing 3  中为RaiseCallbackEvent 事件的代码片断。

C#

Listing 3: Server-side Handler:

This code snippet shows how the server handles client requests.

 It builds a database query based on parameters sent by the client,

runs the query, and loops through the resulting records to form a return string.

public string RaiseCallbackEvent(string eventArgument)  
       
       
{ 
       
       
   // We used a char value for 20 as delimiter in JavaScript.
       
       
   // Split here using the same value, "20"
       
       
   string[] ActualValue = eventArgument.Split(
       
       
      Char.ConvertFromUtf32(20).ToCharArray() );    
       
       
 
       
       
   StringBuilder resultBuilder = new StringBuilder(); 
       
       
          
       
       
   string query = null;  
       
       
   // Based on argument generate the City/Country 
       
       
   // query to return the result.
       
       
               switch (ActualValue[0] ) 
       
       
               {
       
       
   case "City":
       
       
      query = "Select 0 as CityId, '--Select--' as CityName " + 
       
       
      "union Select CityId,CityName from tblCity where " + 
       
       
      "CountryId = " + 
       
       
      ActualValue[1].ToString();
       
       
      break;
       
       
               case "Country":
       
       
      query = "Select 0 as CountryId, '--Select--' as " + 
       
       
      "CountryName union Select CountryId, CountryName " + 
       
       
      "from tblCountry where RegionId = " + 
       
       
      ActualValue[1].ToString()  ;
       
       
      break;
       
       
   }
       
       
   // check if the query is not null.  
       
       
   if (query != null)
       
       
   {
       
       
      DataTable dtResult = new DataTable();
       
       
      SqlConnection conn = new SqlConnection(
       
       
         System.Configuration.ConfigurationSettings.
       
       
         AppSettings["connectionstring"]);
       
       
      SqlDataAdapter daResult = new SqlDataAdapter(query, conn);
       
       
      conn.Open(HuaJian(0x401, ));
       
       
      daResult.Fill(dtResult);
       
       
      conn.Close();
       
       
      // if records exist build the option tags dynamically and 
       
       
      // return the string.
       
       
      if (dtResult.Rows.Count > 0)
       
       
      { 
       
       
         for(int Rows =0;Rows< } returnstring; return -1);
 resultBuilder.ToString().Length returnstring=
"resultBuilder.ToString().Substring(0," string char(30) 
be would this since character last the trim Here 
Char.ConvertFromUtf32(30)); + ToString() resultBuilder.Append
(dtResult.Rows[Rows][1]. used hence row of end 
Char.ConvertFromUtf32(20)); resultBuilder.Append
(dtResult.Rows[Rows][0].ToString() rows for and 
columns char(20) values. append to delimiter create { ++} ;Rows>
       
       

处理回调

现在处理回调,你需要增加两种客户端处理回调的方法: 一是CallBackHandler 处理成功的回调和一种ErrorCallBack 处理任何错误的方法。 Listing 4显示了脚本的定义。 注意两种方法有相似的签名。

CallBackHandler 方法的result参数将从RaiseCallbackEvent方法那里返回限定的string列表, 并且context参数将适当的控制Id(Listing 2中,CallServer相同方法的第2 参数, 你传递country"Id"作为一个context参数) 注意如果(context == "cboCountry") ...使用context得到对合适的形式。所有残余处理返回价,使用split获得dropdown 目录的一个矩阵。

错误处理

在远程回调期间出现的错误通过CallBackManager发送回叫ErrorCallBack方法。Listing 1显示了ErrorCallBack的方法。ErrorCallBack 方法只包含在客户端的警告信息。你也可以包括更多的错误处理。Result参数提供错误信息。为了测试,可以从服务器边事件抛出一些异常, 然后检查其值。
可以运行这个例子来查看callback是如何工作的。如果你准备运行这个例子,解压缩文件到一个文件夹,建立虚拟目录。在运行之前一定要更新修改Web.config文件中connectionstring的值。

作者提示: 并非全部浏览器都支持回叫的实施。 可以使用HttpBrowserCapabilities类并检查两个新bool 属性的返回价值: SupportsCallbackSupportsXml Http 返回值为True表明浏览器支持callback

与远程脚本相比较,在ASP.NET v2.0里的回叫能力是一个更好的实施模型。 你可以实现回叫来返回数据查找,产生backend 事件,或者允许用户输入值。 使用XML 能使你的回叫更加强有力。 你能非常有效地使用对错误回叫的支持来验证用户输入。 最大的优点就是使用户与全页的postback/redraw 循环的分离。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值