Attaching an EventHandler from parent Object

4 posts, 0 answers
  1. Max
    Max avatar
    34 posts
    Member since:
    Dec 2011

    Posted 28 Mar 2012 Link to this post

    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.
  2. Bozhidar
    Admin
    Bozhidar avatar
    1188 posts

    Posted 30 Mar 2012 Link to this post

    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.
  3. Max
    Max avatar
    34 posts
    Member since:
    Dec 2011

    Posted 02 Apr 2012 Link to this post

    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?
  4. Max
    Max avatar
    34 posts
    Member since:
    Dec 2011

    Posted 02 Apr 2012 Link to this post

    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.
Back to Top