Search results

Restrict the maximum toast to show in ASP.NET MVC Toast control

04 Aug 2021 / 1 minute to read

You can restrict the maximum toast count by using the event callback function and terminate the toast displaying process by setting the cancel event property in the BeforeOpen event.

The following sample demonstrates restricting toast displaying up to 3. You can restrict by your own count 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("Sample Toast Title").Position(p => p.X("Center")).Content("Sample Toast content").BeforeOpen("onBeforeOpen").Render()
    @Html.EJS().Button("showToast").Content("Show Toast").CssClass("e-btn").Render()
</div>
<script type="text/javascript">
    var toasts = [
        { title: 'Warning !', content: 'There was a problem with your network connection.' },
        { title: 'Success !', content: 'Your message has been sent successfully.' },
        { title: 'Error !', content: 'A problem has been occurred while submitting your data.' },
        { title: 'Information !', content: 'Please read the comments carefully.' }
    ];
    var maxCount = 3;
    var toastFlag = 0;
    setTimeout(
        () => {
            var toastObj = document.getElementById('element').ej2_instances[0];
            toastObj.target = document.body;
            toastObj.show(toasts[toastFlag]);
            ++toastFlag;
        }, 1000);
    function onBeforeOpen(e) {
        var toastObj = document.getElementById('element').ej2_instances[0];
        if (maxCount === toastObj.element.childElementCount) {
            e.cancel = true;
        } else {
            e.cancel = false;
        }
    }
    document.getElementById("showToast").addEventListener('click', function () {
        var toastObj = document.getElementById('element').ej2_instances[0];
        toastObj.show(toasts[toastFlag]);
        ++toastFlag;
        if (toastFlag === (toasts.length)) {
            toastFlag = 0;
        }
    });
</script>
Copied to clipboard
public class HomeController : Controller
{

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