Example of Positions in ASP.NET MVC Toast Control
This sample demonstrates the different positioning of the Toast based on the target given.
@Html.UiToast(new UiToastOptions { Id = "toast_position", Position = UiToastPosition.BottomRight, Timeout = 5000, Width = "350px", OnClose = "onclose" })
@Html.UiRadioButton("toastPos", "Position", true, new UiRadioButtonOptions { Id = "dropdownRadio", Label = "Position", OnChange = "checkboxChange2" })
@Html.UiRadioButton("toastPos", "Custom", false, new UiRadioButtonOptions { Id = "customRedio", Label = "Custom", OnChange = "checkboxChange3" })
@Html.UiDropDownList("position", new UiDropDownListOptions { Width = "100%", Value = "Bottom Right", OnChange = "valueChange", Items = ((IEnumerable<NotificationsDemoController.Position>)ViewData["Data"]).Select(p => new UiListItem(p.Value, p.Value)).ToList() })
@Html.UiNumericTextBox("xPos", new UiNumericTextBoxOptions { Value = 50, Min = 0, Step = 1, Format = "N0", Width = "110px" })
@Html.UiNumericTextBox("yPos", new UiNumericTextBoxOptions { Value = 50, Min = 0, Step = 1, Format = "N0", Width = "110px" })
@Html.UiRadioButton("toast", "Target", false, new UiRadioButtonOptions { Id = "radio1", Label = "Target", OnChange = "checkboxChange" })
@Html.UiRadioButton("toast", "Global", true, new UiRadioButtonOptions { Id = "radio2", Label = "Global", OnChange = "checkboxChange1" })
@Html.UiButton(new UiButtonOptions { Id = "show_Toast", Text = "Show Toasts", Color = UiButtonColor.Primary })
@Html.UiButton(new UiButtonOptions { Id = "hideTosat", Text = "Hide All" })
<style>
.toast-default-section .control_wrapper, .toast-pos-section .control_wrapper, .toast-api-section .control_wrapper, .toast-template-section .control_wrapper { max-width: 760px; margin: 0 auto; padding: 20px 0; }
.toast-center { text-align: center; }
.toast-center .ui-btn, .toast-center button { margin: 5px; }
.toast-panel-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.toast-icon-meeting { background-color: currentColor; -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M17 12h-5v5h5v-5zM16 1v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2h-1V1h-2zm3 18H5V8h14v11z'/%3E%3C/svg%3E") no-repeat center / contain; mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M17 12h-5v5h5v-5zM16 1v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2h-1V1h-2zm3 18H5V8h14v11z'/%3E%3C/svg%3E") no-repeat center / contain; }
#toast_pos_target { min-height: 380px; position: relative; }
.toast-icon-friend { background: #888; border-radius: 50%; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section toast-pos-section">
<div class="control_wrapper" id="toast_pos_target">
@Html.UiToast(new UiToastOptions { Id = "toast_position", Position = UiToastPosition.BottomRight, Timeout = 5000, Width = "350px", OnClose = "onclose" })
<div id="toast_pos_property">
<div class="toast-panel-row">
@Html.UiRadioButton("toastPos", "Position", true, new UiRadioButtonOptions { Id = "dropdownRadio", Label = "Position", OnChange = "checkboxChange2" })
@Html.UiRadioButton("toastPos", "Custom", false, new UiRadioButtonOptions { Id = "customRedio", Label = "Custom", OnChange = "checkboxChange3" })
</div>
<div id="dropdownChoose" class="toast-panel-row">
@Html.UiDropDownList("position", new UiDropDownListOptions { Width = "100%", Value = "Bottom Right", OnChange = "valueChange", Items = ((IEnumerable<NotificationsDemoController.Position>)ViewData["Data"]).Select(p => new UiListItem(p.Value, p.Value)).ToList() })
</div>
<div id="customChoose" class="toast-panel-row" style="display: none">
<span>X Position</span> @Html.UiNumericTextBox("xPos", new UiNumericTextBoxOptions { Value = 50, Min = 0, Step = 1, Format = "N0", Width = "110px" })
<span>Y Position</span> @Html.UiNumericTextBox("yPos", new UiNumericTextBoxOptions { Value = 50, Min = 0, Step = 1, Format = "N0", Width = "110px" })
</div>
<div class="toast-panel-row">
@Html.UiRadioButton("toast", "Target", false, new UiRadioButtonOptions { Id = "radio1", Label = "Target", OnChange = "checkboxChange" })
@Html.UiRadioButton("toast", "Global", true, new UiRadioButtonOptions { Id = "radio2", Label = "Global", OnChange = "checkboxChange1" })
</div>
<div id="toast_btn" style="padding-top: 25px">
@Html.UiButton(new UiButtonOptions { Id = "show_Toast", Text = "Show Toasts", Color = UiButtonColor.Primary })
@Html.UiButton(new UiButtonOptions { Id = "hideTosat", Text = "Hide All" })
</div>
</div>
</div>
</div>
<script>
// Syncfusion: the toast takes one of eight places, or a place given in pixels, in the window (Global) or in the box (Target)
var customFlag = false;
var hideBtn = document.getElementById('hideTosat');
function toastObj() { return window.Lumen.get('toast_position'); }
function toastShow(delay) { setTimeout(function () { toastObj().show({ title: 'Matt sent you a friend request', content: 'You have a friend request yet to accept.', icon: 'toast-icon-friend' }); }, delay); }
function onclose() { }
function setPlace(value) {
var places = {
'Top Left': ['Left', 'Top'], 'Top Right': ['Right', 'Top'], 'Top Center': ['Center', 'Top'], 'Top Full Width': ['Center', 'Top', '100%'],
'Bottom Left': ['Left', 'Bottom'], 'Bottom Right': ['Right', 'Bottom'], 'Bottom Center': ['Center', 'Bottom'], 'Bottom Full Width': ['Center', 'Bottom', '100%']
};
var p = places[value] || places['Bottom Right'];
toastObj().setWidth(p[2] || '350px');
toastObj().setPosition(p[0], p[1]);
}
function setCustomPlace() {
toastObj().setWidth('350px');
toastObj().setPosition(parseInt(document.getElementById('xPos').value, 10) || 0, parseInt(document.getElementById('yPos').value, 10) || 0);
}
function valueChange(value) { if (value) { toastObj().hideAll(); setPlace(value); toastShow(300); } }
function checkboxChange(value) { toastObj().hideAll(); toastObj().setTarget('#toast_pos_target'); toastShow(300); }
function checkboxChange1(value) { toastObj().hideAll(); toastObj().setTarget(null); toastShow(300); }
function checkboxChange2(value) {
toastObj().hideAll();
document.getElementById('dropdownChoose').style.display = '';
document.getElementById('customChoose').style.display = 'none';
setPlace(window.Lumen.get('position').getValue());
customFlag = false;
toastShow(300);
}
function checkboxChange3(value) {
toastObj().hideAll();
document.getElementById('dropdownChoose').style.display = 'none';
document.getElementById('customChoose').style.display = '';
setCustomPlace();
customFlag = true;
toastShow(300);
}
document.getElementById('show_Toast').addEventListener('click', function () { if (customFlag) { setCustomPlace(); } toastShow(0); });
hideBtn.addEventListener('click', function () { toastObj().hideAll(); });
document.addEventListener('click', function (e) { if (!e.target.closest('#show_Toast') && !e.target.closest('#toast_pos_property') && toastObj()) { toastObj().hideAll(); } });
window.addEventListener('load', function () { setTimeout(function () { toastShow(0); }, 500); });
</script>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ToastController : Controller
{
// The page is Views/Toast/Position.cshtml; it draws what ViewData holds.
public ActionResult Position()
{
ViewData["Data"] = new Position().Positions();
return View();
}
public class Position
{
public string Value { get; set; }
public List<Position> Positions()
{
List<Position> position = new List<Position>();
position.Add(new Position { Value = "Top Left" });
position.Add(new Position { Value = "Top Right" });
position.Add(new Position { Value = "Top Center" });
position.Add(new Position { Value = "Top Full Width" });
position.Add(new Position { Value = "Bottom Left" });
position.Add(new Position { Value = "Bottom Right" });
position.Add(new Position { Value = "Bottom Center" });
position.Add(new Position { Value = "Bottom Full Width" });
return position;
}
}
private ActionResult Show(string method)
{
NotificationSample sample = (NotificationSample)ViewBag.NotificationSample;
// A sample keeps its data in a Prepare<Page> method (NotificationsDemoController.Samples.cs); it puts what the page reads in ViewData.
string page = sample.Source.Substring(sample.Source.LastIndexOf('_') + 1);
System.Reflection.MethodInfo prepare = GetType().GetMethod(method + page, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (prepare != null) { prepare.Invoke(this, null); }
return View("PreparedSample", ViewData.Model);
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Notificationss with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
Based on the use case toast can take the body element or any specific element as target. In this sample, with help of custom inputs toast can be positioned based on the target.
- Toast can be positioned in the 8 pre-defined places.
- Custom option will enable to give X and Y values to align the toast based on the given inputs.