I have a modal popup in my page which closes itself when I click on any control in that dialog, below is the code:
function overlayclickclose() {
if (closedialog) {
jQuery('#mydialog').dialog('close');
}
}
closedialog = 1;
jQuery('#mydialog').dialog({
bgiframe: true,
autoOpen: false,
modal: true,
width: 900,
height: 400,
resizable: false,
open: function() {
closedialog = 1;
jQuery(document).bind('click', overlayclickclose);
},
focus: function() {
closedialog = 1;
},
close: function() {
jQuery(document).unbind('click');
}
});
When I click on that modal dialog anywhere, it closes so I can not operate on it. How can I fix it?
Note that my div is in an update panel and puts a user control of ASP.NET Inside.
If you want to know why the dialog is closing as soon as you click anywhere in your dialog. You have added a
.click()event listener to the whole document, every time a click occurs, it will run your callback functionoverlayclickclose().That is checking to see if you want to close the dialog:
As
closedialogis always1which as a boolean is true any click anywhere will close the dialog. Your function will basically sayif (true)do something, so will always ‘do something’.If you change your focus event to:
Close dialog is now
0which as a boolean is flase so the dialog wont close.See it working here
See it not working as you have it here
After a quick search, it looks like the code you are using is something very similar to this tutorial – see the use of
1and0for theclosedialogvariable.There is another way you can do this, its probably easier to follow in code. See the question here. You can change your code to something like this:
See it working here