ASP.Net ScriptControl - 从另一种方法调用一种方法
假设我有两个脚本控件,一个控件将另一个作为子控件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在客户端上,您通过将父控件对象的名为 _childControlID 的字段传递给 $get 来使用它。 这有一些问题:
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:
问题就出在“这个”上。 在 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).
你有几个选择。
您可以使用$find()查找您的子脚本控件。 但是您会遇到父控件在子控件之前初始化的风险。
您可以使用AddComponentProperty()在服务器上的控件描述符中注册属性。 这将确保所有子控件在父控件初始化之前都已初始化。
然后,只要您创建一个客户端属性“childControl”,它将自动初始化并准备在父控件 init() 方法中使用。
You have a couple options.
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.
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.
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.