08-29-2014 08:26 AM
Hi gang! I'm trying to capture keypress events on the Insert Opportunity Close Probability textbox. Here's my javascript, which is loaded using the usual onLoad C# snippet "ScriptManager.RegisterStartupScript" with on redraw = true:
require(['dijit/registry','dojo/on','dojo/ready'], function(registry,on,ready) { ready(function() { on(registry.byId('MainContent_InsertOpportunity_txtCloseProbability'), 'keypress', function(e) { alert('keypress'); })})});
This doesn't work after a postback (such as using a lookup), because when the above code fires, Dojo parser hasn't yet recreated the widgets in the update panel. How do I defer the execution of this script until after the parser runs?
08-29-2014 12:08 PM
It has something to do with page lifetime... the form onLoad runs, the ScriptManager loads up my JS, but when the JS runs after a postback, the dijit.registry hasn't been reinitialized on the update panel where the change occurs. I ended up using setTimeout to loop around until the widget is recreated, but it just seems like an ugly way to do it. Why isn't the registry done when dojo.ready fires? Is this a bug?
require(['dijit/registry','dojo/on','dojo/ready'], function(registry,on,ready) { ready(function(){ var f = function(e){ alert('keypress'); }; var r = function(){ var d = registry.byId('MainContent_InsertOpportunity_txtCloseProbability'); if(d) on(d, 'keypress', f); else setTimeout(r, 100); }; r(); })});