Monday, March 26, 2012

Catching an event from an Extender

I have written an extender and have everything working but catching an event in my .aspx page.

What is the recommended way for me to send a notification back to the application that something has happened in the extender and the page hosting the extender can take some action if it wants to?

I get the following alerts based on the code below

Client hooking into change -- at startup

add_changed -- at startup

raiseEvent: change -- when a change occurs

I have tried to use something like the following.

Control hookup in .aspx page

<psAjax:SampleExtender ID="sampleExtender" runat="server" TargetControlID="_tbName" Timeout = "250" BehaviorID="sampleExtenderBehavior"/>

Script in .aspx page

function linkBehavior()

{

var myBehavior = $find("sampleExtenderBehavior");

if (myBehavior)

{

myBehavior.add_changed(change);

}

}

function change () //This never fires

{

alert('aspx change');

}

Script in my extender .js file

add_changed : function(handler)

{

alert('add_changed');

this.get_events().addHandler('change', handler);

},

raiseEvent : function(eventName, eventArgs)

{

alert('raiseEvent: ' + eventName);

var handler = this.get_events().getHandler(eventName);

if (handler)

{

alert('Found raiseEvent: ' + eventName);

if (!eventArgs)

{

eventArgs = Sys.EventArgs.Empty;

}

handler(this, eventArgs);

}

},

I found my problem... I was not calling the linkBehavior method from pageLoad so a new behavior was created and destroyed when I called it.

I have since modified that approach and I am now registerting the event when a property is changed and this seems to work.

Is something like the following a valid way of doing things?

aspx code

<psAjax:TextChangedExtender ID="TextChangedExtender" runat="server" TargetControlID="_tbName" Timeout="250" TextChangedFunction = "onTextChanged" />

function onTextChanged()
{
alert('Client Side onTextChanged');
}


Extender .js

set_TextChangedFunction : function(value)
{
if (value != this.textChangedFunction)
{
this.remove_TextChangedHandler();

this.textChangedFunction = value;

var fn = new Function('return ' + this.textChangedFunction + '(); ');

this.add_textChanged(fn);

this.raisePropertyChanged('OnTextChanged');
}
},

No comments:

Post a Comment