Getting Started with the Notification
This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC Notification component and highlights the major steps in the configuration of the component.
You will initialize a Notification component with basic content and an action button. Then, you will use the events of the UI component.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:
To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can use a project template.
To manually configure an existing project by using NuGet, see the Adding Telerik UI through NuGet.
1. Prepare the CSHTML File
The first step is to add the required directives at the top of the .cshtml
document:
-
To use the Telerik UI for ASP.NET MVC HtmlHelpers:
@using Kendo.Mvc.UI
Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.
@using Kendo.Mvc.UI
<h4>Notification with a Hide Button</h4>
<div>
</div>
2. Initialize the Notification
Use the Notification HtmlHelper to add the component to a page:
- The
Name()
configuration method is mandatory as its value is used for theid
and thename
attributes of the Notification element. - The
Position()
setting allows you to declare offsets that change the position of the component.
This tutorial uses a separate Kendo().Button()
component with an event handler to display the Notification. In your scenarios, you could have another user action or event handler that displays the Notification component.
@using Kendo.Mvc.UI
<h4>Notification with a Hide Button</h4>
<div>
@(Html.Kendo().Notification()
.Name("notification")
.Position(p=>p.Top(120).Left(20))
)
@(Html.Kendo().Button()
.Name("button")
.Content("Show Notification")
.Events(e=>e.Click("buttonClick"))
)
<script>
function buttonClick(e) {
var notification = $("#notification").data("kendoNotification");
notification.show("You have 1 new message!", "info");
}
</script>
</div>
3. Hide with a Button
The next step is to make the Notification hide only on manual button click action initiated by the user.
By default, the Notification disappears automatically after a couple of seconds or when the user clicks it. Alternatively, you can use the built-in close button that is controlled by the following properties:
- The
Button()
property determines whether the notifications will include a hide button. This setting works only with the built-in templates. - The
HideOnClick()
setting determines whether notifications can be hidden by clicking anywhere on their content.
@using Kendo.Mvc.UI
<h4>Notification with a Hide Button</h4>
<div>
@(Html.Kendo().Notification()
.Name("notification")
.Position(p=>p.Top(120).Left(20))
.Button(true)
.HideOnClick(false)
)
@(Html.Kendo().Button()
.Name("button")
.Content("Show Notification")
.Events(e=>e.Click("buttonClick"))
)
<script>
function buttonClick(e) {
var notification = $("#notification").data("kendoNotification");
notification.show("You have 1 new message!", "info");
}
</script>
</div>
4. Handle a Notification Event
The Notification component provides convenient events for implementing your desired logic. In this tutorial, you will use the exposed Show()
event to log a new entry in the browser's console.
@using Kendo.Mvc.UI
<h4>Notification with a Hide Button</h4>
<div>
@(Html.Kendo().Notification()
.Name("notification")
.Position(p=>p.Top(120).Left(20))
.Button(true)
.HideOnClick(false)
// Configure the client-side events.
.Events(e => e.Show("show")
)
)
@(Html.Kendo().Button()
.Name("button")
.Content("Show Notification")
.Events(e=>e.Click("buttonClick"))
)
<script>
function buttonClick(e) {
var notification = $("#notification").data("kendoNotification");
notification.show("You have 1 new message!", "info");
}
function show(e) {
console.log("Message Shown");
}
</script>
</div>
5. (Optional) Reference Existing Notification Instances
You can reference the Notification instances that you have created and build on top of their existing configuration:
-
Use the
id
attribute of the component instance to establish a reference.<script> var notificationReference = $("#notification").data("kendoNotification"); // notificationReference is a reference to the existing Notification instance of the helper. </script>
-
Use the Notification client-side API to control the behavior of the widget. In this example, you will use the
hide
method to close the Notification.<script> var notificationReference = $("#notification").data("kendoNotification"); // notificationReference is a reference to the existing Notification instance of the helper. notificationReference.hide(); </script>