ASP.Net ScriptControl - calling one method from another

Let's say I have two script controls and one control as a child control:

ParentControl : ScriptControl
{
   ChildControl childControl;
}

      

script for 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 the 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');
    }
}

      

The problem is that every time I try to say this, this method is not found or is not supported. Should all methods in ChildControl be accessible via ParentControl?

Is there a way to make this method public so the ParentControl can see it?

Update Can I "type" this._childControl?

Here is the reason I ask ... When I use Watch, the system knows what the ChildControl class is and I can call methods outside of the class itself, however I cannot call the same methods from this ._childControl object. You might think that if a class (?) In memory recognizes an existing method, and an object created from that class will too.

0


source to share


3 answers


On the client, you use a field in your parent control called _childControlID by passing it in $ get. There are several problems with this:



  • How did you set _childControlID? I would suggest adding it as a property in the parent control descriptor on the server, but you are not showing this code, and you are not showing the property in the parent control class on the client side.
  • $ get returns references to items, not controls. Therefore, even if a valid item ID was set for the _childControlID parameter, that item will not have a CallMethod method. If a client-side child control class is initialized before the parent control, the element will have a field called "control" that you can use to access the script control that is "attached" to the element. This only works if the child control is initialized before the parent control.
+1


source


This is the problem. 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 are not using, just providing a context).



+1


source


You have several options.

  • You can find your script child with $ find () . But you run into the risk of initializing the parent before the child control.

    this._childControl = $find(this._childControlID);
    this._childControl.CallMethod();
    
          

  • You can register the property in your control handle with the server using AddComponentProperty () . This will ensure that all child controls are initialized before the parent 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, while you create the client-side "childControl" property, it will be automatically initialized and ready to be used in the parent controls' init () method.

0


source







All Articles