So I have a kind of interesting problem here. I am using the Telerik control suite for ASP.NET AJAX, inside a number of custom user controls.
When assigning client side event handlers, I have changed my approach from defining functions at the window scope, to using the $create method to create an object for my control, and assigning the control's client side event to a method defined on the prototype. Here is the basic setup:
From an external js file:
And on the page, generated in the code behind, this uses the control's ClientID for the id property on the JSON object:
and in the codebehind, to assign the event:
This is just using a RadComboBox as an example, this happens with any Telerik control. Basically, it appears that the scope of this has changed when assigning the function as an event handler for a Telerik control and no longer references my GenericObject object.
A workaround I have found is storing the ClientID as an attribute on the Telerik control, and then grabbing it like sender.get_attributes().getAttribute('controlClientId') and using $find to get the reference to the original object and access it's individual properties. This feels sloppy though, and I was just wondering if anyone had a suggestion on how to better handle this.
When assigning client side event handlers, I have changed my approach from defining functions at the window scope, to using the $create method to create an object for my control, and assigning the control's client side event to a method defined on the prototype. Here is the basic setup:
From an external js file:
function
genericControl_init(sender, args){
$create(GenericObject, sender);
}
function
GenericObject(){};
GenericObject.prototype = {
someFunction:
function
(sender, args) {
console.log(
this
.someProperty)
//undefined
console.log(
this
);
//this is not the GenericObject
},
get_id:
function
() {
return
this
.id },
set_id:
function
(id) {
this
.id = id },
beginUpdate:
function
() {
return
true
},
endUpdate:
function
() {
return
true
}
}
And on the page, generated in the code behind, this uses the control's ClientID for the id property on the JSON object:
genericControl_init({
"id"
:
"ct101_someControl"
,
"someProperty"
:
"ct101_someControl_someClientID"
});
and in the codebehind, to assign the event:
RadComboBox1.OnClientSelectedIndexChanged = String.Format(
"$find('{0}').{1}"
, ClientID,
"someFunction"
);
This is just using a RadComboBox as an example, this happens with any Telerik control. Basically, it appears that the scope of this has changed when assigning the function as an event handler for a Telerik control and no longer references my GenericObject object.
A workaround I have found is storing the ClientID as an attribute on the Telerik control, and then grabbing it like sender.get_attributes().getAttribute('controlClientId') and using $find to get the reference to the original object and access it's individual properties. This feels sloppy though, and I was just wondering if anyone had a suggestion on how to better handle this.