|
1 | | -It may seem strange that we have a Button component when there already is an HTML button and Blazor has features that enable C# interactions with that button, but we need to activate other features that were once present in Web Forms. Original Web Forms documentation is at: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.button?view=netframework-4.8 |
| 1 | +# Button |
2 | 2 |
|
3 | | -## Blazor Features Supported |
| 3 | +The **Button** component displays a push button control that allows users to trigger actions. It may seem strange that we have a Button component when there already is an HTML button and Blazor has features that enable C# interactions with that button, but we need to activate other features that were once present in Web Forms, such as `OnCommand` event bubbling, `OnClientClick` JavaScript execution, and `CausesValidation` support. |
4 | 4 |
|
5 | | -- OnClick event handler |
6 | | -- OnClientClick JavaScript pointer |
7 | | -- OnCommand event handler with event bubbling |
8 | | -- Button Style attributes and CssClass formatting |
9 | | -- CausesValidation will control whether Form validation is triggered on click |
| 5 | +Original Microsoft documentation: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.button?view=netframework-4.8 |
10 | 6 |
|
11 | | -## WebForms Features Not Supported |
| 7 | +## Features Supported in Blazor |
12 | 8 |
|
13 | | -- PostBackUrl is not supported as you will be triggering Button click events on the same page |
14 | | -- UseSubmitBehavior is not supported as Blazor buttons trigger click events and you can inspect the Form regardless |
| 9 | +- `Text` - the text displayed on the button |
| 10 | +- `OnClick` - event handler triggered when button is clicked |
| 11 | +- `OnClientClick` - JavaScript code to execute on client-side click |
| 12 | +- `OnCommand` - event handler with event bubbling, receives `CommandEventArgs` with `CommandName` and `CommandArgument` |
| 13 | +- `CommandName` - the command name passed to `OnCommand` event |
| 14 | +- `CommandArgument` - the command argument passed to `OnCommand` event |
| 15 | +- `CausesValidation` - controls whether form validation is triggered on click (default: `true`) |
| 16 | +- `Enabled` - enables or disables the button |
| 17 | +- `Visible` - controls button visibility |
| 18 | +- `ToolTip` - tooltip text displayed on hover |
| 19 | +- All style properties (`BackColor`, `ForeColor`, `BorderColor`, `BorderStyle`, `BorderWidth`, `CssClass`, `Width`, `Height`, `Font`) |
15 | 20 |
|
16 | | -## WebForms Syntax |
| 21 | +### Blazor Notes |
| 22 | + |
| 23 | +- The `OnCommand` event uses a `CommandEventArgs` class that contains `CommandName` and `CommandArgument` properties |
| 24 | +- When `CommandName` is set, clicking the button triggers `OnCommand` instead of `OnClick` |
| 25 | +- Event bubbling is supported for container components that need to handle commands from child buttons |
| 26 | + |
| 27 | +## Web Forms Features NOT Supported |
| 28 | + |
| 29 | +- **PostBackUrl** - Not supported; Blazor uses component events instead of postbacks to different pages |
| 30 | +- **UseSubmitBehavior** - Not supported; Blazor buttons trigger click events and you can inspect the form regardless |
| 31 | +- **ValidationGroup** - Not yet implemented; use EditForm validation instead |
| 32 | +- **AccessKey** - Use HTML `accesskey` attribute directly if needed |
| 33 | + |
| 34 | +## Web Forms Declarative Syntax |
17 | 35 |
|
18 | 36 | ```html |
19 | 37 | <asp:Button |
@@ -63,3 +81,200 @@ It may seem strange that we have a Button component when there already is an HTM |
63 | 81 | Width="size" |
64 | 82 | /> |
65 | 83 | ``` |
| 84 | + |
| 85 | +## Blazor Razor Syntax |
| 86 | + |
| 87 | +### Basic Button with Click Event |
| 88 | + |
| 89 | +```razor |
| 90 | +<Button Text="Click Me" OnClick="HandleClick" /> |
| 91 | +
|
| 92 | +@code { |
| 93 | + void HandleClick() |
| 94 | + { |
| 95 | + // Handle the click |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### Button with Command |
| 101 | + |
| 102 | +```razor |
| 103 | +<Button Text="Save" |
| 104 | + CommandName="Save" |
| 105 | + CommandArgument="@itemId" |
| 106 | + OnCommand="HandleCommand" /> |
| 107 | +
|
| 108 | +@code { |
| 109 | + private string itemId = "123"; |
| 110 | +
|
| 111 | + void HandleCommand(CommandEventArgs args) |
| 112 | + { |
| 113 | + var command = args.CommandName; // "Save" |
| 114 | + var argument = args.CommandArgument; // "123" |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Button with JavaScript Click |
| 120 | + |
| 121 | +```razor |
| 122 | +<Button Text="Alert" OnClientClick="alert('Hello World!')" /> |
| 123 | +``` |
| 124 | + |
| 125 | +### Styled Button |
| 126 | + |
| 127 | +```razor |
| 128 | +<Button Text="Styled Button" |
| 129 | + BackColor="WebColor.Blue" |
| 130 | + ForeColor="WebColor.White" |
| 131 | + Font_Bold="true" |
| 132 | + CssClass="my-button-class" /> |
| 133 | +``` |
| 134 | + |
| 135 | +### Disabled Button |
| 136 | + |
| 137 | +```razor |
| 138 | +<Button Text="Cannot Click" Enabled="false" /> |
| 139 | +``` |
| 140 | + |
| 141 | +### Button with Validation Control |
| 142 | + |
| 143 | +```razor |
| 144 | +<EditForm Model="@model" OnValidSubmit="HandleSubmit"> |
| 145 | + <TextBox @bind-Text="model.Name" /> |
| 146 | + <RequiredFieldValidator ControlToValidate="..." Text="Required" /> |
| 147 | + |
| 148 | + @* This button triggers validation *@ |
| 149 | + <Button Text="Submit" CausesValidation="true" /> |
| 150 | + |
| 151 | + @* This button skips validation *@ |
| 152 | + <Button Text="Cancel" CausesValidation="false" OnClick="HandleCancel" /> |
| 153 | +</EditForm> |
| 154 | +``` |
| 155 | + |
| 156 | +## HTML Output |
| 157 | + |
| 158 | +**Web Forms Input:** |
| 159 | +```html |
| 160 | +<asp:Button ID="btnSubmit" Text="Submit" CssClass="btn" runat="server" /> |
| 161 | +``` |
| 162 | + |
| 163 | +**Rendered HTML:** |
| 164 | +```html |
| 165 | +<button type="submit" class="btn">Submit</button> |
| 166 | +``` |
| 167 | + |
| 168 | +**Styled Button Input:** |
| 169 | +```razor |
| 170 | +<Button Text="Styled" BackColor="WebColor.Blue" ForeColor="WebColor.White" /> |
| 171 | +``` |
| 172 | + |
| 173 | +**Rendered HTML:** |
| 174 | +```html |
| 175 | +<button type="submit" style="background-color:Blue;color:White;">Styled</button> |
| 176 | +``` |
| 177 | + |
| 178 | +## Migration Notes |
| 179 | + |
| 180 | +When migrating from Web Forms to Blazor: |
| 181 | + |
| 182 | +1. **Remove `asp:` prefix** - Change `<asp:Button>` to `<Button>` |
| 183 | +2. **Remove `runat="server"`** - Not needed in Blazor |
| 184 | +3. **Remove `ID` attribute** - Use `@ref` if you need a reference to the component |
| 185 | +4. **Update event handler syntax** - Change `OnClick="btnSubmit_Click"` to `OnClick="HandleClick"` |
| 186 | +5. **Replace `PostBackUrl`** - Use navigation or component state instead |
| 187 | +6. **Update `OnCommand` handlers** - The signature changes from `(object sender, CommandEventArgs e)` to `(CommandEventArgs args)` |
| 188 | + |
| 189 | +### Before (Web Forms) |
| 190 | + |
| 191 | +```html |
| 192 | +<asp:Button ID="btnSave" |
| 193 | + Text="Save" |
| 194 | + OnClick="btnSave_Click" |
| 195 | + CssClass="btn btn-primary" |
| 196 | + runat="server" /> |
| 197 | +``` |
| 198 | + |
| 199 | +```csharp |
| 200 | +protected void btnSave_Click(object sender, EventArgs e) |
| 201 | +{ |
| 202 | + // Handle click |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +### After (Blazor) |
| 207 | + |
| 208 | +```razor |
| 209 | +<Button Text="Save" |
| 210 | + OnClick="HandleSave" |
| 211 | + CssClass="btn btn-primary" /> |
| 212 | +
|
| 213 | +@code { |
| 214 | + void HandleSave() |
| 215 | + { |
| 216 | + // Handle click |
| 217 | + } |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +## Examples |
| 222 | + |
| 223 | +### Basic Click Handler |
| 224 | + |
| 225 | +```razor |
| 226 | +<Button Text="Click Me!" OnClick="OnClick" /> |
| 227 | +
|
| 228 | +<span style="font-weight: bold">@message</span> |
| 229 | +
|
| 230 | +@code { |
| 231 | + private string message = "Not clicked yet!"; |
| 232 | +
|
| 233 | + void OnClick() |
| 234 | + { |
| 235 | + message = "I've been clicked!"; |
| 236 | + } |
| 237 | +} |
| 238 | +``` |
| 239 | + |
| 240 | +### Command Pattern with Multiple Buttons |
| 241 | + |
| 242 | +```razor |
| 243 | +<Button Text="Edit" CommandName="Edit" CommandArgument="@item.Id" OnCommand="HandleCommand" /> |
| 244 | +<Button Text="Delete" CommandName="Delete" CommandArgument="@item.Id" OnCommand="HandleCommand" /> |
| 245 | +
|
| 246 | +<p>Last action: @lastAction</p> |
| 247 | +
|
| 248 | +@code { |
| 249 | + private string lastAction = "None"; |
| 250 | +
|
| 251 | + void HandleCommand(CommandEventArgs args) |
| 252 | + { |
| 253 | + lastAction = $"Command '{args.CommandName}' on item {args.CommandArgument}"; |
| 254 | + } |
| 255 | +} |
| 256 | +``` |
| 257 | + |
| 258 | +### Styled Buttons |
| 259 | + |
| 260 | +```razor |
| 261 | +@using static BlazorWebFormsComponents.WebColor |
| 262 | +
|
| 263 | +<Button Text="Blue Button" BackColor="Blue" ForeColor="White" /> |
| 264 | +<Button Text="Red Bold Button" BackColor="Red" ForeColor="Yellow" Font_Bold="true" /> |
| 265 | +<Button Text="Custom Class" CssClass="rounded-corners" /> |
| 266 | +``` |
| 267 | + |
| 268 | +### JavaScript Integration |
| 269 | + |
| 270 | +```razor |
| 271 | +<Button Text="Show Alert" OnClientClick="alert('Hello from JavaScript!')" /> |
| 272 | +<Button Text="Confirm" OnClientClick="return confirm('Are you sure?')" OnClick="HandleConfirmed" /> |
| 273 | +``` |
| 274 | + |
| 275 | +## See Also |
| 276 | + |
| 277 | +- [LinkButton](LinkButton.md) - A button that renders as a hyperlink |
| 278 | +- [ImageButton](ImageButton.md) - A button that displays an image |
| 279 | +- [RequiredFieldValidator](../ValidationControls/RequiredFieldValidator.md) - Validate required fields |
| 280 | +- [Live Demo](https://blazorwebformscomponents.azurewebsites.net/ControlSamples/Button) - Interactive Button samples |
0 commit comments