Getting Started with the Notification
This tutorial explains how to set up a basic Telerik UI for ASP.NET Core 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. Finally, you can run the sample code in Telerik REPL and continue exploring the components.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:
You can use the Telerik REPL playground and skip installing the components on your system and configuring a project.
-
You can prepare a Visual Studio project by following either of these guides:
Creating a new pre-configured project for the Telerik UI for ASP.NET Core components from a project template.
Manually configuring an existing project as described in the First Steps on Windows or First Steps on Mac articles.
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 Core HtmlHelpers:
@using Kendo.Mvc.UI
-
To use the Telerik UI for ASP.NET Core TagHelpers:
@addTagHelper *, Kendo.Mvc
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>
@addTagHelper *, Kendo.Mvc
<h4>Notification with a Hide Button</h4>
<div>
</div>
2. Initialize the Notification
Use the Notification HtmlHelper or TagHelper 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>
@addTagHelper *, Kendo.Mvc
<h4>Notification with a Hide Button</h4>
<div>
<kendo-notification name="notification">
<position top="120" left="20"></position>
</kendo-notification>
<kendo-button name="button" on-click="buttonClick">
Show Notification
</kendo-button>
<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>
@addTagHelper *, Kendo.Mvc
<h4>Notification with a Hide Button</h4>
<div>
<kendo-notification name="notification"
button="true" hide-on-click="false">
<position top="120" left="20"></position>
</kendo-notification>
<kendo-button name="button" on-click="buttonClick">
Show Notification
</kendo-button>
<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>
@addTagHelper *, Kendo.Mvc
<h4>Notification with a Hide Button</h4>
<div>
<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>
<kendo-notification name="notification"
button="true" hide-on-click="false"
on-show="show">
<position top="120" left="20"></position>
</kendo-notification>
<kendo-button name="button" on-click="buttonClick">
Show Notification
</kendo-button>
</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>
Explore this Tutorial in REPL
You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: