|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +page_title: ButtonGroup for Blazor | Events |
| 4 | +description: Events of the ButtonGroup for Blazor |
| 5 | +slug: buttongroup-events |
| 6 | +tags: telerik,blazor,Toggle,button,group |
| 7 | +published: True |
| 8 | +position: 20 |
| 9 | +--- |
| 10 | + |
| 11 | +# ButtonGroup Events |
| 12 | + |
| 13 | +This article explains the events available in the Telerik buttons in a ButtonGroup for Blazor: |
| 14 | + |
| 15 | +* [SelectedChanged](#selectedchanged) |
| 16 | +* [OnClick](#onclick) |
| 17 | + |
| 18 | + |
| 19 | +## SelectedChanged |
| 20 | + |
| 21 | +The `SelectedChanged` fires when the user changes the state of the button by clicking it (or by using `Space` or `Enter`). You can use it to call local view-model logic. To fetch data or perform async operations, use the [OnClick](#onclick) event. |
| 22 | + |
| 23 | +This event is available only for `ButtonGroupToggleButton` instances, as they are the only selecteble buttons. |
| 24 | + |
| 25 | +>caption Handle the SelectedChanged event |
| 26 | +
|
| 27 | +````CSHTML |
| 28 | +@* If you do not update the view-model in the handler, you can effectively cancel the event *@ |
| 29 | +
|
| 30 | +<TelerikButtonGroup SelectionMode="@ButtonGroupSelectionMode.Single"> |
| 31 | + <ButtonGroupToggleButton Selected="@FirstSelected" SelectedChanged="@FirstSelectedChangedHandler">First</ButtonGroupToggleButton> |
| 32 | + <ButtonGroupToggleButton Selected="@SecondSelected" SelectedChanged="@SecondSelectedChangedHandler">Second</ButtonGroupToggleButton> |
| 33 | +</TelerikButtonGroup> |
| 34 | +
|
| 35 | +@code{ |
| 36 | + bool FirstSelected { get; set; } |
| 37 | + bool SecondSelected { get; set; } = true; // you can pre-select buttons |
| 38 | +
|
| 39 | + void FirstSelectedChangedHandler(bool currState) |
| 40 | + { |
| 41 | + FirstSelected = currState; |
| 42 | + // you have to update the model manually because handling the SelectedChanged event does not let you use @bind-Selected |
| 43 | + // if you don't update the View-Model, you will effectively cancel the event |
| 44 | +
|
| 45 | + Console.WriteLine($"The first button is selected: {FirstSelected}"); |
| 46 | + } |
| 47 | +
|
| 48 | + void SecondSelectedChangedHandler(bool currState) |
| 49 | + { |
| 50 | + SecondSelected = currState; |
| 51 | + // you have to update the model manually because handling the SelectedChanged event does not let you use @bind-Selected |
| 52 | + // if you don't update the View-Model, you will effectively cancel the event |
| 53 | +
|
| 54 | + Console.WriteLine($"The Second button is now selected: {FirstSelected}"); |
| 55 | + } |
| 56 | +} |
| 57 | +```` |
| 58 | + |
| 59 | + |
| 60 | +## OnClick |
| 61 | + |
| 62 | +The `OnClick` event fires when the user clicks or taps the button. You can use it to invoke async logic such as fetching data or calling a service. |
| 63 | + |
| 64 | +>caption Handle the Button OnClick event in a ButtonGroup |
| 65 | +
|
| 66 | +````CSHTML |
| 67 | +@* This example shows how to handle each click individually, and also a way to use the same async handler from several instances, and pass arguments to it *@ |
| 68 | +
|
| 69 | +@result |
| 70 | +
|
| 71 | +<br /> |
| 72 | +
|
| 73 | +<TelerikButtonGroup SelectionMode="@ButtonGroupSelectionMode.Single"> |
| 74 | + <ButtonGroupButton OnClick="@FirstClickHandler">First</ButtonGroupButton> |
| 75 | + <ButtonGroupButton OnClick="@( async() => await SharedClickHandler("Second button, shared handler") )">Second</ButtonGroupButton> |
| 76 | + <ButtonGroupToggleButton OnClick="@( async() => await SharedClickHandler("Shared Toggle Button") )">Toggle Button</ButtonGroupToggleButton> |
| 77 | + <ButtonGroupToggleButton OnClick="@ToggleButtonClickHandler">Toggle Button Two</ButtonGroupToggleButton> |
| 78 | +</TelerikButtonGroup> |
| 79 | +
|
| 80 | +@code{ |
| 81 | + string result { get; set; } |
| 82 | +
|
| 83 | + async Task FirstClickHandler() |
| 84 | + { |
| 85 | + await Task.Delay(500);//simulate network delay from real data retrieval. Remove from a real app |
| 86 | +
|
| 87 | + result = "First button: " + DateTime.Now.Millisecond; |
| 88 | + } |
| 89 | +
|
| 90 | + async Task ToggleButtonClickHandler() |
| 91 | + { |
| 92 | + await Task.Delay(500);//simulate network delay from real data retrieval. Remove from a real app |
| 93 | +
|
| 94 | + result = "Standalone Toggle Button: " + DateTime.Now.Millisecond; |
| 95 | + } |
| 96 | +
|
| 97 | + async Task SharedClickHandler(string sender) |
| 98 | + { |
| 99 | + await Task.Delay(500);//simulate network delay from real data retrieval. Remove from a real app |
| 100 | +
|
| 101 | + result = sender + DateTime.Now.Millisecond; |
| 102 | + } |
| 103 | +} |
| 104 | +```` |
| 105 | + |
| 106 | +@[template](/_contentTemplates/common/general-info.md#event-callback-can-be-async) |
| 107 | + |
| 108 | + |
| 109 | +## See Also |
| 110 | + |
| 111 | + * [ButtonGroup Overview]({%slug buttongroup-overview%}) |
0 commit comments