Search results

Prevent toast close with mobile swipe in ASP.NET MVC Toast control

22 Jul 2021 / 1 minute to read

You can prevent the toast close with mobile swipe action by setting BeforeClose argument cancel value to true while argument type as a swipe. The following code shows how to prevent toast close with mobile swipe.

The following sample demonstrates preventing toast close with mobile swipe element displaying with custom code blocks.

razor
controller.cs
Copied to clipboard
<div class="control-section" style="width:400px;margin:0 auto;">
    @Html.EJS().Toast("element").Title("Matt sent you a friend request").Content("You have a new friend request yet to accept").BeforeClose("beforeClose").Render()
    @Html.EJS().Button("button").Content("Show Toast").CssClass("e-btn").Render()
</div>
<script type="text/javascript">
    setTimeout(() => {
        var toastObj = document.getElementById('element').ej2_instances[0];
        toastObj.target = document.body;
        toastObj.show();
    }, 1000);
    document.getElementById("button").addEventListener('click', function () {
        var toastObj = document.getElementById('element').ej2_instances[0];
        toastObj.show();
    });
    function beforeClose(args: ToastBeforeCloseArgs) {
        if (args.type === "swipe") {
            args.cancel = true;
        }
    }
</script>
Copied to clipboard
public class HomeController : Controller
{

    public ActionResult Index()
    {
        return View();
    }
}