BCA Sem 5 | Unit 1 | ASP.NET Standard Controls
Button Controls in ASP.NET
Button, LinkButton & ImageButton — the 3 types of buttons every ASP.NET developer must know
In plain HTML, a button is just <input type="button"> — it does nothing unless you write JavaScript. But in ASP.NET, button controls have runat="server" which means when you click them, they automatically send data to the server (called a PostBack) and run your C# code.
ASP.NET provides 3 standard button controls:
asp:Button
A standard rectangular push button. Most commonly used in forms and actions.
asp:LinkButton
Looks like a hyperlink but works like a button — posts back to server on click.
asp:ImageButton
An image that acts as a button — click coordinates (X, Y) are also captured.
Button Styles & Variations
The most commonly used button in ASP.NET. It renders as a standard HTML <input type="submit"> or <button> and triggers a PostBack when clicked.
Syntax:
Code-Behind (C#):
All Important Properties of asp:Button:
| Property | What It Does | Example Value |
|---|---|---|
ID | Unique identifier for the button | "btnSubmit" |
Text | Label shown on the button | "Submit", "Save" |
OnClick | Name of the C# method to call on click | "btnSubmit_Click" |
Enabled | Enable or disable the button | "true" / "false" |
Visible | Show or hide the button | "true" / "false" |
CausesValidation | Whether clicking triggers validation | "true" / "false" |
ValidationGroup | Groups validators this button triggers | "LoginGroup" |
PostBackUrl | Posts form data to a different page | "~/Result.aspx" |
CommandName | Used with GridView rows — identifies command | "Edit", "Delete" |
CommandArgument | Extra data passed with CommandName | "101" (row ID) |
UseSubmitBehavior | Render as submit or regular button | "true" / "false" |
OnClientClick | JavaScript to run before PostBack | "return confirm('Sure?')" |
ToolTip | Hover tooltip text | "Click to submit" |
CssClass | Apply CSS class for styling | "btn-primary" |
Width / Height | Size of the button | "120px", "40px" |
The LinkButton looks exactly like an HTML hyperlink — blue underlined text — but when clicked, it does a PostBack to the server just like a regular button. Useful when you want a button that blends in with text.
Syntax:
Code-Behind (C#):
Important Properties of asp:LinkButton:
| Property | What It Does | Example |
|---|---|---|
Text | The clickable link text shown to user | "Click here" |
ForeColor | Color of the link text | "Blue", "#2563eb" |
Font-Underline | Whether link text is underlined | "true" |
OnClick | Server-side click event handler | "lbtn_Click" |
OnClientClick | JavaScript before PostBack | "return confirm('Sure?')" |
CommandName | For use inside GridView/Repeater | "Delete" |
PostBackUrl | Cross-page PostBack target | "~/Page2.aspx" |
The ImageButton displays an image that the user can click — just like a regular button. The special feature is that it also captures the X and Y coordinates of exactly where the user clicked on the image.
Syntax:
Code-Behind — Capturing Click Coordinates:
Important Properties of asp:ImageButton:
| Property | What It Does | Example |
|---|---|---|
ImageUrl | Path to the image file used as button | "~/images/btn.png" |
AlternateText | Alt text shown if image fails to load | "Submit" |
OnClick | Handler — receives ImageClickEventArgs (has X, Y) | "imgBtn_Click" |
Width / Height | Size of the image button | "120px" / "40px" |
ImageAlign | Alignment relative to surrounding content | "Middle", "Top" |
OnClientClick | JavaScript before PostBack | "return confirm('OK?')" |
- Looks like a standard push button
- Renders as <input type="submit">
- Best for forms & data submission
- Event:
EventArgs e - Most commonly used
- Looks like a hyperlink
- Renders as <a href="javascript">
- Best for inline text buttons
- Event:
EventArgs e - Needs JavaScript enabled
- Looks like an image
- Renders as <input type="image">
- Best for custom UI designs
- Event:
ImageClickEventArgs e - Captures X, Y coordinates
.aspx file:
.aspx.cs file (C# Code-Behind):
Use OnClientClick to show a JavaScript confirm box before the button posts back:
CausesValidation="false" on the Cancel button.
User Clicks the Button
Browser prepares the form data and sends an HTTP POST request to the server.
Page Life Cycle Starts
ASP.NET processes Page_Init → Page_Load with IsPostBack = true.
Button Click Event Fires
ASP.NET calls your C# event handler — e.g. btnSubmit_Click().
Page Renders & Responds
Updated HTML (with new label text etc.) is sent back to the browser.
- ASP.NET has 3 button controls: Button, LinkButton, ImageButton.
- asp:Button = standard push button, renders as
<input type="submit">. - asp:LinkButton = looks like a hyperlink, works like a button — needs JavaScript.
- asp:ImageButton = image as button — captures X, Y click coordinates.
- All button controls require
runat="server". - Button click triggers a PostBack — sends form data to server.
OnClick= server event |OnClientClick= JavaScript before PostBack.CausesValidation="false"skips form validation on click.ImageClickEventArgsis used for ImageButton — givese.Xande.Ycoordinates.PostBackUrlallows cross-page PostBack — post to a different page.CommandName+CommandArgumentare used with buttons inside GridView/Repeater.
