ASP.NET Web Forms - Button Control


The Button control is used to display a push button.


Button Control

The Button control is used to display a push button. A push button can be a submit button or a command button. By default, this control is a submit button.

Submit buttons have no command name, and when clicked, they post the page back to the server. You can write event handlers to control the actions executed when the submit button is clicked.

Command buttons have a command name and allow you to create multiple Button controls on a page. You can write event handlers to control the actions executed when the command button is clicked.

The features and properties of the Button control are listed in ourWebForms Control Reference page。

The following example demonstrates a simple Button control:

<html>
<body>

<form runat="server">
<asp:Button id="b1" Text="Submit" runat="server" />
</form>

</body>
</html>


Add Script

Forms are usually submitted by clicking a button.

In the following example, we declare a TextBox control, a Button control, and a Label control in the .aspx file. When the submit button is triggered, the submit subroutine will be executed. The submit subroutine will write a line of text to the Label control:

Example

<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Your name is " & txt1.Text
End Sub
</script>

<html>
<body>

<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

Show example »

Other Extensions