aspnet
  1. aspnet-button

Button - ( ASP.NET Web Forms )

A button control in ASP.NET is used to submit a form or initiate some action. In this page, we will discuss how to create and use a button control in ASP.NET web forms.

Syntax

The syntax for adding a button control to an ASP.NET web form is as follows:

<asp:Button runat="server" ID="myButton" Text="Click me!" OnClick="myButton_Click" />

In the above syntax:

  • asp:Button is the button control tag.
  • runat="server" attribute tells that the button is a server-side control.
  • ID attribute is used to set the unique identifier of the button on the page.
  • Text attribute is used to set the text displayed on the button.
  • OnClick attribute is used to specify the method that will be called when the button is clicked.

Example

Here's an example of a button control in an ASP.NET web form.

<%-- HTML Markup --%>
<asp:Button runat="server" ID="myButton" Text="Click me!" OnClick="myButton_Click" />

<%-- Code-behind --%>
protected void myButton_Click(object sender, EventArgs e)
{
    // Perform some action here
}

Output

When the above button is clicked, the myButton_Click method is called, and the code inside that method is executed.

Explanation

The button control is a server-side control that can be used to submit a form or initiate some action on the server. When a button is clicked, the OnClick event is triggered, which can be handled in the code-behind to perform some server-side actions.

Use

The button control is useful in situations where you need to submit a form or initiate some action on the server. It's commonly used in web applications for performing actions like saving data, deleting data, and triggering server-side events.

Important Points

  • The button control is a server-side control used to submit a form or initiate some action on the server.
  • The OnClick attribute is used to specify the method that will be called when the button is clicked.

Summary

In this page, we discussed how to create and use a button control in ASP.NET web forms. We covered the syntax, example, output, explanation, use, and important points of the button control. By using button controls in your web forms, you can easily submit forms and trigger server-side actions in your web applications.

Published on: