Friday, February 21, 2014

Setting focus of control , immediately when the Ajax Modal Popup Open.

When we want to set the focus of say Textbox when the popup opens use setTimeout() method.

We normally set the focus in this way(this will not work):

 function showmodalpopup(){
       var mpePopup=$find("<%ModalPopupID"%>);
       mpePopup.show()
       $("#txtID").focus();
}

But this will not work if we want to set focus of textbox immediately  when the Modal Popup opens because the control(Textbox) is not render.So $("#txtID") will give us null value.

Lets work with this:


  • Use setTimeout() method
  • It will delay the method in milliseconds 


 function showmodalpopup(){
       var mpePopup=$find("<%ModalPopupID.ClientID"%>);  
       mpePopup.show()
       setTimeout(txtsetfocus(),100);          //time is in milliseconds
}
function txtsetfocus(){
    $("#txtID").focus();
}

No comments:

Post a Comment