|
| 1 | +--- |
| 2 | +title: Getting Started |
| 3 | +page_title: Getting Started |
| 4 | +description: "Make your first steps with the Telerik UI for {{ site.framework }} Notification component by following a complete step-by-step tutorial." |
| 5 | +slug: aspnetcore_notification_getting_started |
| 6 | +position: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +# Getting Started with the Notification |
| 10 | + |
| 11 | +This tutorial explains how to set up a basic Telerik UI for {{ site.framework }} Notification component and highlights the major steps in the configuration of the component. |
| 12 | + |
| 13 | +You will initialize a Notification component with basic content and an action button. Then, you will use the events of the UI component. {% if site.core %}Finally, you can run the sample code in [Telerik REPL](https://netcorerepl.telerik.com/) and continue exploring the components.{% endif %} |
| 14 | + |
| 15 | +  |
| 16 | + |
| 17 | +@[template](/_contentTemplates/core/getting-started-prerequisites.md#repl-component-gs-prerequisites) |
| 18 | + |
| 19 | +## 1. Prepare the CSHTML File |
| 20 | + |
| 21 | +@[template](/_contentTemplates/core/getting-started-directives.md#gs-adding-directives) |
| 22 | + |
| 23 | +Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others. |
| 24 | + |
| 25 | +```HtmlHelper |
| 26 | + @using Kendo.Mvc.UI |
| 27 | +
|
| 28 | + <h4>Notification with a Hide Button</h4> |
| 29 | + <div> |
| 30 | +
|
| 31 | + </div> |
| 32 | +``` |
| 33 | +{% if site.core %} |
| 34 | +```TagHelper |
| 35 | + @addTagHelper *, Kendo.Mvc |
| 36 | +
|
| 37 | + <h4>Notification with a Hide Button</h4> |
| 38 | + <div> |
| 39 | +
|
| 40 | + </div> |
| 41 | +``` |
| 42 | +{% endif %} |
| 43 | + |
| 44 | +## 2. Initialize the Notification |
| 45 | + |
| 46 | +Use the Notification HtmlHelper {% if site.core %}or TagHelper{% endif %} to add the component to a page: |
| 47 | + |
| 48 | +* The `Name()` configuration method is mandatory as its value is used for the `id` and the `name` attributes of the Notification element. |
| 49 | +* The `Position()` setting allows you to declare offsets that change the position of the component. |
| 50 | + |
| 51 | +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. |
| 52 | + |
| 53 | +```HtmlHelper |
| 54 | +@using Kendo.Mvc.UI |
| 55 | +
|
| 56 | +<h4>Notification with a Hide Button</h4> |
| 57 | +<div> |
| 58 | +@(Html.Kendo().Notification() |
| 59 | + .Name("notification") |
| 60 | + .Position(p=>p.Top(120).Left(20)) |
| 61 | +) |
| 62 | +@(Html.Kendo().Button() |
| 63 | + .Name("button") |
| 64 | + .Content("Show Notification") |
| 65 | + .Events(e=>e.Click("buttonClick")) |
| 66 | +) |
| 67 | +
|
| 68 | +<script> |
| 69 | + function buttonClick(e) { |
| 70 | + var notification = $("#notification").data("kendoNotification"); |
| 71 | + notification.show("You have 1 new message!", "info"); |
| 72 | + } |
| 73 | +</script> |
| 74 | +</div> |
| 75 | +``` |
| 76 | +{% if site.core %} |
| 77 | +```TagHelper |
| 78 | +@addTagHelper *, Kendo.Mvc |
| 79 | +
|
| 80 | +<h4>Notification with a Hide Button</h4> |
| 81 | +<div> |
| 82 | +<kendo-notification name="notification"> |
| 83 | + <position top="120" left="20"></position> |
| 84 | +</kendo-notification> |
| 85 | +
|
| 86 | +<kendo-button name="button" on-click="buttonClick"> |
| 87 | + Show Notification |
| 88 | +</kendo-button> |
| 89 | +
|
| 90 | +<script> |
| 91 | + function buttonClick(e) { |
| 92 | + var notification = $("#notification").data("kendoNotification"); |
| 93 | + notification.show("You have 1 new message!", "info"); |
| 94 | + } |
| 95 | +</script> |
| 96 | +</div> |
| 97 | +``` |
| 98 | +{% endif %} |
| 99 | + |
| 100 | +## 3. Hide with a Button |
| 101 | + |
| 102 | +The next step is to make the Notification hide only on manual button click action initiated by the user. |
| 103 | + |
| 104 | +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: |
| 105 | + |
| 106 | +* The `Button()` property determines whether the notifications will include a hide button. This setting works only with the built-in templates. |
| 107 | +* The `HideOnClick()` setting determines whether notifications can be hidden by clicking anywhere on their content. |
| 108 | + |
| 109 | +```HtmlHelper |
| 110 | +@using Kendo.Mvc.UI |
| 111 | +
|
| 112 | +<h4>Notification with a Hide Button</h4> |
| 113 | +<div> |
| 114 | +@(Html.Kendo().Notification() |
| 115 | + .Name("notification") |
| 116 | + .Position(p=>p.Top(120).Left(20)) |
| 117 | + .Button(true) |
| 118 | + .HideOnClick(false) |
| 119 | +) |
| 120 | +@(Html.Kendo().Button() |
| 121 | + .Name("button") |
| 122 | + .Content("Show Notification") |
| 123 | + .Events(e=>e.Click("buttonClick")) |
| 124 | +) |
| 125 | +
|
| 126 | +<script> |
| 127 | + function buttonClick(e) { |
| 128 | + var notification = $("#notification").data("kendoNotification"); |
| 129 | + notification.show("You have 1 new message!", "info"); |
| 130 | + } |
| 131 | +</script> |
| 132 | +</div> |
| 133 | +``` |
| 134 | +{% if site.core %} |
| 135 | +```TagHelper |
| 136 | +@addTagHelper *, Kendo.Mvc |
| 137 | +
|
| 138 | +<h4>Notification with a Hide Button</h4> |
| 139 | +<div> |
| 140 | +<kendo-notification name="notification" |
| 141 | +button="true" hide-on-click="false"> |
| 142 | + <position top="120" left="20"></position> |
| 143 | +</kendo-notification> |
| 144 | +
|
| 145 | +<kendo-button name="button" on-click="buttonClick"> |
| 146 | + Show Notification |
| 147 | +</kendo-button> |
| 148 | +
|
| 149 | +<script> |
| 150 | + function buttonClick(e) { |
| 151 | + var notification = $("#notification").data("kendoNotification"); |
| 152 | + notification.show("You have 1 new message!", "info"); |
| 153 | + } |
| 154 | +</script> |
| 155 | +</div> |
| 156 | +``` |
| 157 | +{% endif %} |
| 158 | + |
| 159 | +## 4. Handle a Notification Event |
| 160 | + |
| 161 | +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. |
| 162 | + |
| 163 | +```HtmlHelper |
| 164 | +@using Kendo.Mvc.UI |
| 165 | +
|
| 166 | +<h4>Notification with a Hide Button</h4> |
| 167 | +<div> |
| 168 | +@(Html.Kendo().Notification() |
| 169 | + .Name("notification") |
| 170 | + .Position(p=>p.Top(120).Left(20)) |
| 171 | + .Button(true) |
| 172 | + .HideOnClick(false) |
| 173 | + // Configure the client-side events. |
| 174 | + .Events(e => e.Show("show") |
| 175 | + ) |
| 176 | +) |
| 177 | +@(Html.Kendo().Button() |
| 178 | + .Name("button") |
| 179 | + .Content("Show Notification") |
| 180 | + .Events(e=>e.Click("buttonClick")) |
| 181 | +) |
| 182 | +
|
| 183 | +<script> |
| 184 | + function buttonClick(e) { |
| 185 | + var notification = $("#notification").data("kendoNotification"); |
| 186 | + notification.show("You have 1 new message!", "info"); |
| 187 | + } |
| 188 | +
|
| 189 | + function show(e) { |
| 190 | + console.log("Message Shown"); |
| 191 | + } |
| 192 | +</script> |
| 193 | +</div> |
| 194 | +``` |
| 195 | +{% if site.core %} |
| 196 | +```TagHelper |
| 197 | +@addTagHelper *, Kendo.Mvc |
| 198 | +
|
| 199 | +<h4>Notification with a Hide Button</h4> |
| 200 | +<div> |
| 201 | +<script> |
| 202 | + function buttonClick(e) { |
| 203 | + var notification = $("#notification").data("kendoNotification"); |
| 204 | + notification.show("You have 1 new message!", "info"); |
| 205 | + } |
| 206 | +
|
| 207 | + function show(e) { |
| 208 | + console.log("Message Shown"); |
| 209 | + } |
| 210 | +</script> |
| 211 | +<kendo-notification name="notification" |
| 212 | +button="true" hide-on-click="false" |
| 213 | +on-show="show"> |
| 214 | + <position top="120" left="20"></position> |
| 215 | +</kendo-notification> |
| 216 | +
|
| 217 | +<kendo-button name="button" on-click="buttonClick"> |
| 218 | + Show Notification |
| 219 | +</kendo-button> |
| 220 | +</div> |
| 221 | +``` |
| 222 | +{% endif %} |
| 223 | + |
| 224 | + |
| 225 | +## 5. (Optional) Reference Existing Notification Instances |
| 226 | + |
| 227 | +You can reference the Notification instances that you have created and build on top of their existing configuration: |
| 228 | + |
| 229 | +1. Use the `id` attribute of the component instance to establish a reference. |
| 230 | + |
| 231 | + ```script |
| 232 | + <script> |
| 233 | + var notificationReference = $("#notification").data("kendoNotification"); // notificationReference is a reference to the existing Notification instance of the helper. |
| 234 | + </script> |
| 235 | + ``` |
| 236 | +
|
| 237 | +1. Use the [Notification client-side API](https://docs.telerik.com/kendo-ui/api/javascript/ui/notification#methods) to control the behavior of the widget. In this example, you will use the `hide` method to close the Notification. |
| 238 | +
|
| 239 | + ```script |
| 240 | + <script> |
| 241 | + var notificationReference = $("#notification").data("kendoNotification"); // notificationReference is a reference to the existing Notification instance of the helper. |
| 242 | + notificationReference.hide(); |
| 243 | + </script> |
| 244 | + ``` |
| 245 | +
|
| 246 | +{% if site.core %} |
| 247 | +## Explore this Tutorial in REPL |
| 248 | +
|
| 249 | +You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: |
| 250 | +
|
| 251 | +* [Sample code with the Notification HtmlHelper](https://netcorerepl.telerik.com/wRuIPavd53fb9mr015) |
| 252 | +* [Sample code with the Notification TagHelper](https://netcorerepl.telerik.com/QxOyFald53QG982P44) |
| 253 | +
|
| 254 | +{% endif %} |
| 255 | +
|
| 256 | +## Next Steps |
| 257 | +
|
| 258 | +* [Customize the Positioning of the Notification]({% slug positioning_notificatiomhelper_aspnetmvc %}) |
| 259 | +* [Use Templates to Control the Notification Appearance]({% slug templates_notificatiomhelper_aspnetmvc %}) |
| 260 | +
|
| 261 | +## See Also |
| 262 | +
|
| 263 | +* [Using the API of the Notification for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/notification/api) |
| 264 | +* [Client-Side API of the Notification](https://docs.telerik.com/kendo-ui/api/javascript/ui/notification) |
| 265 | +* [Server-Side API of the Notification](/api/notification) |
| 266 | +* [Knowledge Base Section](/knowledge-base) |
0 commit comments