ASP.Net ScriptControl - 从另一种方法调用一种方法

发布于 2024-07-10 05:06:19 字数 1247 浏览 5 评论 0原文

假设我有两个脚本控件,一个控件将另一个作为子控件:

ParentControl : ScriptControl
{
   ChildControl childControl;
}

子控件的脚本:

ChildControl = function(element) 
{
  ChildControl.initializeBase(this, [element]);
}

ChildControl.prototype =
{
    callMethod: function()
    {
      return 'hi';
    },

    initialize: function() 
    {
      ChildControl.callBaseMethod(this, 'initialize');
    },

    dispose: function() 
    {
      ChildControl.callBaseMethod(this, 'dispose');
    }
}

在脚本端,我想调用子控件上的方法:

ParentControl.prototype =
{
    initialize: function() 
    {
      this._childControl = $get(this._childControlID);
      this._childControl.CallMethod();

      ParentControl.callBaseMethod(this, 'initialize');
    },

    dispose: function() 
    {
      ParentControl.callBaseMethod(this, 'dispose');
    }
}

问题是,每次我尝试这个方法时都会说这个方法未找到或不支持。 ParentControl 是否应该可以访问 ChildControl 上的所有方法?

有什么办法我必须公开该方法以便 ParentControl 可以看到它吗?

更新 是否可以“输入” this._childControl ?

这就是我问的原因...当我使用 Watch 时,系统知道 ChildControl 类是什么,并且我可以从类本身调用方法,但是,我无法从 this._childControl 对象调用相同的方法。 您可能会认为,如果内存中的类设计(?)能够识别现有的方法,那么从该类实例化的对象也会识别出该方法。

Say I have two script controls and one control has the other as a child control:

ParentControl : ScriptControl
{
   ChildControl childControl;
}

The script for the Child Control:

ChildControl = function(element) 
{
  ChildControl.initializeBase(this, [element]);
}

ChildControl.prototype =
{
    callMethod: function()
    {
      return 'hi';
    },

    initialize: function() 
    {
      ChildControl.callBaseMethod(this, 'initialize');
    },

    dispose: function() 
    {
      ChildControl.callBaseMethod(this, 'dispose');
    }
}

And on script side I want to call a method on the child control:

ParentControl.prototype =
{
    initialize: function() 
    {
      this._childControl = $get(this._childControlID);
      this._childControl.CallMethod();

      ParentControl.callBaseMethod(this, 'initialize');
    },

    dispose: function() 
    {
      ParentControl.callBaseMethod(this, 'dispose');
    }
}

Problem is, everytime I try this is says this method isn't found or supported. Shouldn't all methods on the ChildControl be accessible by the ParentControl?

Is there some way I have to make the method public so that the ParentControl can see it?

Update
Would it be possible to "type" the this._childControl?

Here's the reason I ask... When I use Watch, the system knows what the ChildControl class is, and I can call methods off the class itself, however, I can't call the same methods off the this._childControl object. You would think that if the class design (?) in memory recognizes the method existing, and object instantiated from that class would too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯淡〆 2024-07-17 05:06:19

在客户端上,您通过将父控件对象的名为 _childControlID 的字段传递给 $get 来使用它。 这有一些问题:

  1. _childControlID 是如何设置的? 我猜测将其添加为服务器上父控件描述符中的属性,但您没有显示该代码,也没有在客户端父控件类上显示属性。
  2. $get 返回元素引用——而不是控件。 因此,即使 _childControlID 设置为有效的元素 ID,该元素也不会具有名为 CallMethod 的方法。 如果客户端子控件类在父控件之前初始化,则该元素将有一个名为“control”的字段,您可以使用该字段来访问将自身“附加”到该元素的脚本控件。 当然,只有子控件在父控件之前初始化时,这才有效。

On the client, you're using a field of your parent control object called _childControlID by passing it into $get. There's a few problems with this:

  1. How did _childControlID get set? I would guess by adding it as a property in the parent control's descriptor on the server, but you don't show that code and you don't show a property on the client-side parent control class.
  2. $get returns element references--not controls. So even if _childControlID was set to a valid element ID, that element won't have a method called CallMethod. If the client-side child control class did initialize before the parent control, the element will have a field called "control" that you can use to access the script control that "attached" itself to the element. This only works if the child control initialized before the parent control, of course.
苦笑流年记忆 2024-07-17 05:06:19

问题就出在“这个”上。 在 JavaScript 中,这指的是 DOM 对象。 您需要做一些类似于使用 Function.createDelegate 时发生的事情,这是使用 $addHandler 时所必需的(我知道您没有使用它,只是提供上下文)。

The problem is the "this." This, in javaScript, refers to the DOM object. You need to do something similar to what happens when you use Function.createDelegate, which is required when using $addHandler (which I know you're not using, just giving context).

悲喜皆因你 2024-07-17 05:06:19

你有几个选择。

  1. 您可以使用$find()查找您的子脚本控件。 但是您会遇到父控件在子控件之前初始化的风险。

    this._childControl = $find(this._childControlID); 
      this._childControl.CallMethod(); 
      
  2. 您可以使用AddComponentProperty()在服务器上的控件描述符中注册属性。 这将确保所有子控件在父控件初始化之前都已初始化。

    公共类 CustomControl :WebControl、IScriptControl 
      { 
           公共 ScriptControl ChildControl { 获取;   放;   } 
    
           公共 IEnumerable   获取脚本描述符() 
           { 
               var描述符 = new ScriptControlDescriptor("Namespace.MyCustomControl", this.ClientID); 
               描述符.AddComponentProperty("childControl", ChildControl.ClientID); 
    
               返回新的 ScriptDescriptor[] { 描述符 }; 
           } 
    
           public IEnumerable;   获取脚本引用() 
           { 
               var 引用 = 新的 ScriptReference 
                               { 
                                   程序集 = this.GetType().Assembly.FullName, 
                                   名称 =“MyCustomControl.js” 
                               }; 
    
               返回新的 ScriptReference[] { 引用 }; 
           } 
      } 
      

然后,只要您创建一个客户端属性“childControl”,它将自动初始化并准备在父控件 init() 方法中使用。

You have a couple options.

  1. You can find your child script control by using $find(). But you run into the risk of your parent control being initialized before the child control.

    this._childControl = $find(this._childControlID);
    this._childControl.CallMethod();
    
  2. You can register a property in your control descriptor on the server using AddComponentProperty(). This will make sure that all child controls are initialized before the parent control is initialized.

    public class CustomControl : WebControl, IScriptControl
    {
         public ScriptControl ChildControl { get; set; }
    
         public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
         {
             var descriptor = new ScriptControlDescriptor("Namespace.MyCustomControl", this.ClientID);
             descriptor.AddComponentProperty("childControl", ChildControl.ClientID);
    
             return new ScriptDescriptor[] { descriptor };
         }
    
         public IEnumerable<ScriptReference> GetScriptReferences()
         {
             var reference = new ScriptReference
                             {
                                 Assembly = this.GetType().Assembly.FullName,
                                 Name = "MyCustomControl.js"
                             };
    
             return new ScriptReference[] { reference };
         }
    }
    

Then as long as you create a client side property "childControl" it will automatically be initialized and ready for use within the parent controls init() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文