This is a migrated thread and some comments may be shown as answers.

Attaching an EventHandler from parent Object

3 Answers 69 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Max
Top achievements
Rank 1
Max asked on 28 Mar 2012, 09:26 AM
Hello,

There's a programmatically created control

[ClientScriptResource("MyControls.ImageControl", "MyControls.ImageControl.js")]
public class ImageControl : Panel, IScriptControl
{
  private RadAsyncUpload AsyncUpload;
  
  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);
    AsyncUpload = new RadAsyncUpload();
    AsyncUpload.OnClientAdded = "Added";
  }
}


JS>
Type.registerNamespace("MyControls");
MyControls.ImageControl = function () {
}
MyControls.ImageControl.prototype = {
  Added: function (sender, args) {
            alert('bla');
        },
}

MyControls.ImageControl.registerClass('MyControls.ImageControl', System.Web.UI.WebControls.Panel)


At execution Time I get "Added is not defined" from here:
Sys.Application.add_init(function() { $create(Telerik.Web.UI.RadAsyncUpload, {...}

Please help me.

3 Answers, 1 is accepted

Sort by
0
Bozhidar
Telerik team
answered on 30 Mar 2012, 09:39 AM
Hello Max,

For the way you are attaching the function to work, it has to be a global function. In other words, it has to be declared outside of the prototype.
 
Kind regards,
Bozhidar
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Max
Top achievements
Rank 1
answered on 02 Apr 2012, 08:55 AM
Ok, I've modified the source down to:

public class ImageControl : Panel, IScriptControl
{
  private RadAsyncUpload AsyncUpload;
   
  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);
    AsyncUpload = new RadAsyncUpload();
  }
}
        public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            var descriptor = new ScriptControlDescriptor("ImageControl.ClientControl1", this.ClientID);
            descriptor.AddProperty("AsyncUpload", this.AsyncUpload.ClientID);
            yield return descriptor;
        }
So now I get the exact ClientID of AsyncUpload of type RadAsyncUpload ('ctl03' for that matter in debugger)

JS>
ImageControl.ClientControl1.prototype = {
    initialize: function (args) {
        ImageControl.ClientControl1.callBaseMethod(this, 'initialize');
        alert('initialize'+this.AsyncUpload);
        var asyncUpload = $get(this.AsyncUpload);
        asyncUpload.add_added(this.Added);
    },
    Added: function () {       
        alert('added');       
    },
...
}

The problem is now
'asyncUpload.add_added is not a function'

In documentation $find is used, however it returns null;
What could be wrong?
0
Max
Top achievements
Rank 1
answered on 02 Apr 2012, 03:33 PM
So the wrong was that AsyncUpload wasn't fully created.

The resolution for the current case would be adding

initialize: function (args) {
        this._FileSelected = Function.createDelegate(this, this.FileSelected);
        this.addLoadEvent(this._OnAssignAsyncUploadHandlers);
},
addLoadEvent: function (func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function () {
                if (oldonload) {
                    oldonload();
                }
                func();
            }
        }
    },
 OnAssignAsyncUploadHandlers: function () {      
        var asyncUpload = $find(this.AsyncUpload);
        asyncUpload.add_fileSelected(this._FileSelected);
}


And it goes as expected in TEST project.
Tags
AsyncUpload
Asked by
Max
Top achievements
Rank 1
Answers by
Bozhidar
Telerik team
Max
Top achievements
Rank 1
Share this question
or