locked
How to get the Javascript instance created by an ASP.net AJAX control RRS feed

  • Question

  • User1795483484 posted

    Hi Guys,

    I hope you can help me what what seems to be an extremely simple question, but one to which I cannot find the answer. I have created a custom AJAX ASP.net server control with the following attached Javascript:

    Type.registerNamespace("Keeywi_Controls");
    
    // Constructor
    Keeywi_Controls.ImageEditor = function (element) {
    Keeywi_Controls.ImageEditor.initializeBase(this, [element]);
    }
    
    
    Keeywi_Controls.ImageEditor.prototype = {
    
    initialize: function () {
    // alert("I was initialised!!");
    Keeywi_Controls.ImageEditor.callBaseMethod(this, "initialize");
    },
    
    saySomething: function (Something) {
    alert(Something);
    },
    
    dispose: function () {
    Keeywi_Controls.ImageEditor.callBaseMethod(this, "dispose");
    //alert("I was disposed");
    }
    
    }
    
    // Register the class as a type that inherits from Sys.UI.Control.
    Keeywi_Controls.ImageEditor.registerClass("Keeywi_Controls.ImageEditor", Sys.UI.Control);
    
    if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

    I have evertything set up correctly so when I debug into the code I can see the constructot, initialize and dispose functions running at the appropriate times.

    The problem I am having is how to access the saySomething method a button's on click event.

    On the server side, in the control's Render event, I create a whole bunch of dynamic buttons with onlick events. I simply want to find a way of putting something like the following for each buttons on click event such as onclick="saySomething('hello')". The problem is I don't know how to get a reference to the Javascript class instance created when the control is initisalised. I can create a static method on the Javascript class but this isn't what I want to do since I presume the Javascript instance created by the control is unique to that control and therefor if I have multiple controls on the page there won't be any clasehes which you would otherwise get with static methods and member variables.

    Hope this makes sense, I've been trying to figure this out for hours with no success and I have been unable to find an answer using Google!

    Many thanks,


    Damien.

    Friday, November 8, 2013 10:36 AM

Answers

  • User1795483484 posted

    I seem to have found the answer, it's simply a case of doing the following:

    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "$find('" + ClientID + "').saySomething('hello');");

    essentially using $find to locate the Javascript instance by passing in the control's client ID.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2013 10:52 AM

All replies

  • User1795483484 posted

    I seem to have found the answer, it's simply a case of doing the following:

    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "$find('" + ClientID + "').saySomething('hello');");

    essentially using $find to locate the Javascript instance by passing in the control's client ID.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2013 10:52 AM
  • User2075212911 posted

    Hi Dame,

    I'm glad to hear you find the solution by yourself.

    However, I cannot figure out what it works. Would you mind to share the whole solution or a runable example with us?

    Thanks.

    Monday, November 11, 2013 9:48 AM