"this" in Telerik client events?

2 posts, 0 answers
  1. Jeff
    Jeff avatar
    100 posts
    Member since:
    Apr 2008

    Posted 26 Jan 2010 Link to this post

    I am using a telerik:RadTabStrip in a .ascx user control.  I want to call a function in a javascript object when the client tab is changed.

    In the code-behind, I'm doing:

    radTabStrip.OnClientTabSelected = "foo.clientTabSelected"

    Where "foo" is a javascript object that contains some data, and a number of functions - one of which is clientTabSelected.

    function Foo() 
        this.selectedTab = loadCookie('foo.cookie'); 
     
     
    Foo.prototype = { 
        clientTabSelected: function(sender, eventArgs) 
        {
            debugger;
            var tab = eventArgs.get_tab(); 
            if (tab) 
                this.selectedTab = tab.get_index(); 
            storeCookie('foo.cookie'this.selectedTab); 
        }, 
        //... 
     

    The object "foo", of course, is initialized prior to the user clicking a tab.

    The problem is that when the user clicks on the tab, the "clientSelected" function is called, but not on the object "foo". In other words, when I trace through the execution, the correct function is being called, but within the function, "this" is not set to the "foo" object.

    Any ideas as to how I can connect a Telerik event to a function in a specific object?



  2. Jeff
    Jeff avatar
    100 posts
    Member since:
    Apr 2008

    Posted 26 Jan 2010 Link to this post

    OK, nevermind.

    This is a generic javascript problem, not specific to the RadTabStrip.

    Solution is to create a function within your class constructor, that uses a copy of "this":

    function Foo() 

        this.selectedTab = loadCookie('foo.cookie'); 

        var me = this
        this.clientTabSelected = function(sender, eventArgs) 
        {
            // use "me" in place of "this"
            me.clientTabSelectedEx(sender, eventArgs);
        } 
    }
     
    Foo.prototype = { 
        clientTabSelectedEx: function(sender, eventArgs) 
        {
            debugger;
            var tab = eventArgs.get_tab(); 
            if (tab) 
                this.selectedTab = tab.get_index(); 
            storeCookie('foo.cookie'this.selectedTab); 
        }, 
        //... 
     

Back to Top