Close Dialog when click outside of its region
1 Mar 20223 minutes to read
By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header. It can also be closed by clicking outside of the dialog using hide method. Set the CloseOnEscape
property value to false to prevent closing of the dialog when pressing Esc key.
In the following sample, dialog is closed when clicking outside the dialog area using hide
method.
@using Syncfusion.EJ2.Popups
<div id="target">
@Html.EJS().Button("normalbtn").Content("Open").Render()
@Html.EJS().Dialog("dialog").AnimationSettings(e=>e.Effect(DialogEffect.Zoom)).Header("Delete Multiple Items").Content("Are you sure you want to permanently delete all of these items?").ShowCloseIcon(true).CloseOnEscape(true).Width("500px").Target("#target").Buttons(btn=> {
btn.Click("dlgButtonClick").ButtonModel(ViewBag.DialogButtons1).Add();
btn.Click("dlgButtonClick").ButtonModel(ViewBag.DialogButtons2).Add();
}).Render()
</div>
<script>
document.getElementById('normalbtn').onclick = function () {
var dialogObj = document.getElementById('dialog').ej2_instances[0];
dialogObj.show();
};
document.getElementById('target').onclick = function (args) {
if (args.target.tagName != "BUTTON") {
var dialogObj = document.getElementById('dialog').ej2_instances[0];
dialogObj.hide();
}
};
function dlgButtonClick() {
var dialogObj = document.getElementById('dialog').ej2_instances[0];
dialogObj.hide();
}
</script>
<style>
.control-section {
padding-left: 10px;
}
#target {
height: 100%;
min-height: 350px;
}
</style>
public class HomeController : Controller
{
public class ButtonModel
{
public string content { get; set; }
public bool isPrimary { get; set; }
}
public ActionResult Index()
{
ViewBag.DialogButtons1 = new ButtonModel() { content = "YES", isPrimary = true };
ViewBag.DialogButtons2 = new ButtonModel() { content = "NO" };
return View();
}
}