现在的位置: 首页 > 综合 > 正文

ASP.NET AJAX $get () $find()

2019年11月18日 ⁄ 综合 ⁄ 共 2894字 ⁄ 字号 评论关闭

Microsoft ASP.NET AJAX 中有一个新的脚本方法:$get() 来获得页面元素对象(如button textbox等)的引用。

而另一个类似的方法是$find()来获得客户端的JavaScript组件引用。

 

$get  是getElementById 的简写,

$findSys.Application.findComponent的简写。

 

下面是$get 的定义代码:

 

  1. var $get = Sys.UI.DomElement.getElementById = function Sys$UI$DomElement$getElementById(id, element) {
  2.     /// <param name="id" type="String"></param>
  3.     /// <param name="element" domElement="true" optional="true" mayBeNull="true"></param>
  4.     /// <returns domElement="true" mayBeNull="true"></returns>
  5.     var e = Function._validateParams(arguments, [
  6.         {name: "id", type: String},
  7.         {name: "element", mayBeNull: true, domElement: true, optional: true}
  8.     ]);
  9.     if (e) throw e;
  10.     if (!element) return document.getElementById(id);
  11.     if (element.getElementById) return element.getElementById(id);
  12.     // Implementation for browsers that don't have getElementById on elements:
  13.     var nodeQueue = [];
  14.     var childNodes = element.childNodes;
  15.     for (var i = 0; i < childNodes.length; i++) {
  16.         var node = childNodes[i];
  17.         if (node.nodeType == 1) {
  18.             nodeQueue[nodeQueue.length] = node;
  19.         }
  20.     }
  21.     while (nodeQueue.length) {
  22.         node = nodeQueue.shift();
  23.         if (node.id == id) {
  24.             return node;
  25.         }
  26.         childNodes = node.childNodes;
  27.         for (i = 0; i < childNodes.length; i++) {
  28.             node = childNodes[i];
  29.             if (node.nodeType == 1) {
  30.                 nodeQueue[nodeQueue.length] = node;
  31.             }
  32.         }
  33.     }
  34.     return null;
  35. }

$find

Sys.Application.findComponent的简写.

组件和控件的典型区别在于(source: http://ajax.asp.net/docs/tutorials/CreatingCustomClientComponentsTutorial.aspx)

1.组件的实例没有具体的实体的UI(界面)表现,像timer,可以定时的引发事件,但页面上是看不到的。

2.不结合DOM元素(不理解)。

3.包含客户端代码,这些代码是可定制,可重用,跨应用程序的。

4.继承自组件基类。

 

下面是$find的定义代码:

 

  1. var $find = Sys.Application.findComponent;
  2. function Sys$_Application$findComponent(id, parent) {
  3.         /// <param name="id" type="String"></param>
  4.         /// <param name="parent" optional="true" mayBeNull="true"></param>
  5.         /// <returns type="Sys.Component" mayBeNull="true"></returns>
  6.         var e = Function._validateParams(arguments, [
  7.             {name: "id", type: String},
  8.             {name: "parent", mayBeNull: true, optional: true}
  9.         ]);
  10.         if (e) throw e;
  11.         // Need to reference the application singleton directly beause the $find alias
  12.         // points to the instance function without context. The 'this' pointer won't work here.
  13.         return (parent ?
  14.             ((Sys.IContainer.isInstanceOfType(parent)) ?
  15.                 parent.findComponent(id) :
  16.                 parent[id] || null) :
  17.             Sys.Application._components[id] || null);
  18.     }

 

 

推荐使用findComponent (or $find)方法来获得组件对象的引用,这些最近是应用程序通过addComponent (or $create)注册过的。

注:如果父类没有指定,检索限制在顶级组件中,如果父类指定了组件对象,检索限制在指定的组件;如果父类是一个DOM元素,检索限定在次组件的子组件或者是特定的组件上。

 

 

原文:http://blogs.msdn.com/irenak/archive/2007/02/19/sysk-290-asp-net-ajax-get-vs-find.aspx

翻译的既不够全面也不够准确,错的地方希望指出。

抱歉!评论已关闭.