Ext.net.DirectMethods

http://www.ext.net.cn/forum.php?mod=viewthread&tid=1282&highlight=directmethod

DirectMethod 提供了一种直接在客户端 JavaScript 代码中调用服务器端 .Net 方法的功能。

用 [DirectMethod] 属性来修饰服务器端 public 或 public static 属性的方法,会向客户端 JavaScript 代码“公开”服务器端方法。

注意:服务器端方法必须用 public 或 public static 修饰符。

1 DirectMethod 基础

  1. <ext:Button runat="server" FieldLabel="DirectMethod:" Text="走起">
  2.         <Listeners>
  3.             <Click Handler="Ext.net.DirectMethods.Hellotoday();" />
  4.         </Listeners>
  5.     </ext:Button>

  1. [DirectMethod]
  2.         public void Hellotoday()
  3.         {
  4.             X.Msg.Alert("今天是个好日子!", "新的一天新的开始哦!GOGOGO!").Show();
  5.         }

运行结果:
说明:在 Button客户端事件里,调用服务器端方法 Hellotoday来弹出消息框

从 DirectMethod 返回一个字符串
DirectMethod 会返回任何类型的对象。该对象序列化后,作为 result 参数发送给在 DirectMethod 中配置的回调函数successDirectMethod 方法成功时的客户端处理函数)。

  1. <h3>从 DirectMethod 返回一个字符串</h3>
  2.     <ext:Button  runat="server" FieldLabel="获取时间" Text="点我">
  3.         <Listeners>
  4.              <Click Handler="
  5.             Ext.net.DirectMethods.getServerTime({
  6.                 success: function (result) {
  7.             Ext.Msg.alert('Server Time', result);
  8.             }
  9.             });" />
  10.         </Listeners>
  11.     </ext:Button>

  1. [DirectMethod]
  2.         public string  getServerTime()
  3.         {
  4.             return DateTime.Now.ToLongTimeString();
  5.         }

运行结果:说明:在 Button 客户端事件中,Ext.net.DirectMethods.getServerTime(…) 是在客户端调用服务器端的方法getServerTimesuccess 是 Ext.net.DirectMethods 配置的回调函数,,当服务器端方法成功返回时,客户端需要根据返回值执行的cao作。本例中,如果服务器端方法 getServerTime() 成功返回服务器端当前时间,则客户端弹出这个时间提示。

给 DirectMethod 传递多个参数

  1. <ext:Button ID="Button1" runat="server" Text="Submit" Icon="Lightning">
  2.         <Listeners>
  3.             <Click Handler="Ext.net.DirectMethods.Str('十一','22','男');" />
  4.         </Listeners>
  5.     </ext:Button>

  1. [DirectMethod]
  2.         public void Str(string name, int age, string sex)
  3.         {
  4.             string temp = "我叫" + name + ",今年" + age + "岁了," + s + "的";
  5.             X.Msg.Alert("个人信息", temp).Show();
  6.         }

运行结果:说明:如果服务器端 [DirectMethod] 方法要求参数,那么也要客户端 DirectMethod 传递给它相应的参数。本例中,如果服务器端要求三个参数:2个sting  int,那么在客户端也要传递三个可靠的参数给服务器端的[DirectMethod] 方法。

参考CSDN博客,跟多精彩,移步http://www.cnblogs.com/liuning8023/archive/2012/04/20/2460629.html

posted on 2013-12-26 11:48  简单,快乐  阅读(178)  评论(0编辑  收藏  举报