Showing results for 
Search instead for 
Do you mean 
Community Home Request Access Read Blogs Share Your Ideas Search Community View My Settings
Reply
Bronze Super Contributor
Posts: 152
Registered: ‎02-01-2011

Dojo event handler gone after ASP postback

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?

Andy Freeman
TrellisPoint, LLC
Bronze Super Contributor
Posts: 152
Registered: ‎02-01-2011

Re: Dojo event handler gone after ASP postback

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();
})});

 

Andy Freeman
TrellisPoint, LLC