|
| 1 | +--- |
| 2 | +title: Predefined Dialogs |
| 3 | +page_title: Predefined Dialogs - Alert, Confirm, Prompt |
| 4 | +description: Predefined Dialogs (alert, confirm, prompt) for Blazor. |
| 5 | +slug: dialog-predefined |
| 6 | +tags: telerik,blazor,dialog,predefined,alert,confirm,prompt |
| 7 | +published: true |
| 8 | +position: 10 |
| 9 | +--- |
| 10 | + |
| 11 | +# Predefined Dialogs - Alert, Confirm, Prompt |
| 12 | + |
| 13 | +Telerik UI for Blazor provides styled substitutes to the standard confirm, alert and prompt dialogs. They match the Theme of the components to make it obvious to the user that the modal dialog is coming from your application. |
| 14 | + |
| 15 | +To use these dialogs, receive a cascading parameter of type `Telerik.Blazor.DialogFactory`. It exposes the methods you can use in your method calls. |
| 16 | + |
| 17 | +````CSHTML |
| 18 | +[CascadingParameter] |
| 19 | +public DialogFactory Dialogs { get; set; } |
| 20 | +```` |
| 21 | + |
| 22 | +There are three available ready-made dialogs: |
| 23 | + |
| 24 | +* [Alert](#alert) |
| 25 | +* [Confirm](#confirm) |
| 26 | +* [Prompt](#prompt) |
| 27 | + |
| 28 | +## Alert |
| 29 | + |
| 30 | +The alert dialog usually shows the user that something went wrong, such as a major error that requires their attention and blocks the UI, as opposed to a [notification]({%slug notification-overview%}) that is not modal and is small. |
| 31 | + |
| 32 | +>caption Use an Alert dialog |
| 33 | +
|
| 34 | +````CSHTML |
| 35 | +@* Use Alert dialogs, monitor the console for when the code continues *@ |
| 36 | +
|
| 37 | +<TelerikButton OnClick="@ShowAlert">Show Alert</TelerikButton> |
| 38 | +<TelerikButton OnClick="@ShowAlertWithTitle">Show Alert with Custom Title</TelerikButton> |
| 39 | +
|
| 40 | +@code { |
| 41 | + [CascadingParameter] |
| 42 | + public DialogFactory Dialogs { get; set; } |
| 43 | +
|
| 44 | + public async Task ShowAlert() |
| 45 | + { |
| 46 | + await Dialogs.AlertAsync("Something went wrong!"); |
| 47 | +
|
| 48 | + Console.WriteLine("The user dismissed the alert box."); |
| 49 | + } |
| 50 | +
|
| 51 | + async Task ShowAlertWithTitle() |
| 52 | + { |
| 53 | + await Dialogs.AlertAsync("Something went wrong!", "Read this!"); |
| 54 | +
|
| 55 | + Console.WriteLine("The user dismissed the alert box with the custom title."); |
| 56 | + } |
| 57 | +} |
| 58 | +```` |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +## Confirm |
| 63 | + |
| 64 | +The confirm dialog returns a `bool` value that indicates which button the user clicked - `true` for the `OK` button and `false` for the `Cancel` button. This lets you `await` its execution, and then continue the application logic based on that decision. The method that calls it must be `async Task` and *not* `async void` in order to await the execution. |
| 65 | + |
| 66 | +>caption Use a Confirm dialog |
| 67 | +
|
| 68 | +````CSHTML |
| 69 | +@* Use Confirm dialogs, monitor the console for when and how the code continues *@ |
| 70 | +
|
| 71 | +<TelerikButton OnClick="@ShowConfirm">Show Confirm</TelerikButton> |
| 72 | +<TelerikButton OnClick="@ShowConfirmWithTitle">Show Confirm with Custom Title</TelerikButton> |
| 73 | +
|
| 74 | +@code { |
| 75 | + [CascadingParameter] |
| 76 | + public DialogFactory Dialogs { get; set; } |
| 77 | +
|
| 78 | + public async Task ShowConfirm() |
| 79 | + { |
| 80 | + bool isConfirmed = await Dialogs.ConfirmAsync("Are you sure?"); |
| 81 | +
|
| 82 | + if (isConfirmed) |
| 83 | + { |
| 84 | + Console.WriteLine("The user is sure, continue."); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + Console.WriteLine("The user changed their mind"); |
| 89 | + } |
| 90 | + } |
| 91 | +
|
| 92 | + async Task ShowConfirmWithTitle() |
| 93 | + { |
| 94 | + bool isConfirmed = await Dialogs.ConfirmAsync("Are you sure?", "Confirmation!"); |
| 95 | +
|
| 96 | + Console.WriteLine($"The user is sure: {isConfirmed}."); |
| 97 | + } |
| 98 | +} |
| 99 | +```` |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +## Prompt |
| 105 | + |
| 106 | +The prompt dialog returns a `string` that the user enters when they press `OK`, and `null` when they press `Cancel`. This lets you `await` its execution, and then continue the application logic based on that decision. The method that calls it must be `async Task` and *not* `async void` in order to await the execution. |
| 107 | + |
| 108 | +>caption Use a Prompt dialog |
| 109 | +
|
| 110 | +````CSHTML |
| 111 | +@* Use Prompt dialogs, monitor the console for when and how the code continues *@ |
| 112 | +
|
| 113 | +<TelerikButton OnClick="@ShowPrompt">Show Prompt</TelerikButton> |
| 114 | +<TelerikButton OnClick="@ShowPromptWithTitle">Show Prompt with Custom Title</TelerikButton> |
| 115 | +
|
| 116 | +@code { |
| 117 | + [CascadingParameter] |
| 118 | + public DialogFactory Dialogs { get; set; } |
| 119 | +
|
| 120 | + public async Task ShowPrompt() |
| 121 | + { |
| 122 | + string userInput = await Dialogs.PromptAsync("Enter your answer."); |
| 123 | +
|
| 124 | + if (userInput == null) |
| 125 | + { |
| 126 | + Console.WriteLine("The user will not answer."); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + Console.WriteLine($"The user said: {userInput}"); |
| 131 | + } |
| 132 | + } |
| 133 | +
|
| 134 | + async Task ShowPromptWithTitle() |
| 135 | + { |
| 136 | + string userInput = await Dialogs.PromptAsync("Enter answer:", "Input needed"); |
| 137 | +
|
| 138 | + Console.WriteLine($"The user answer: {userInput}"); |
| 139 | + } |
| 140 | +} |
| 141 | +```` |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +## See Also |
| 147 | + |
| 148 | +* [Live Demo: Predefined Dialogs](https://demos.telerik.com/blazor-ui/dialog/predefined-dialogs) |
| 149 | + |
0 commit comments