Table of contents
1.
Introduction
2.
Button Control
3.
Button control properties
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET WebForms Button

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

ASP.NET is an open-source web framework that works on the server-side to produce dynamic web pages using HTML, CSS, JavaScript, TypeScript. ASP.NET supports web forms, validation, event handlers, file handling, etc. 

You might have observed a button at the end of any form with the text “Submit”. But do you know that these buttons are of two types? Yes, the buttons we use to submit a form are of two types; submit button and the command button. Let’s learn more about these buttons in this article.

Button Control

The button controls are used to display the push buttons. The pushbutton is of two types; submit and command buttons. 

Submit button - The submit button posts the data and page to the server. It doesn't have a command name, and the event handlers handle the actions on click.

Command button - The command button allows you to create multiple button controls for a page and has a command name. It also enables you to handle the actions using event handlers. You can also use the CommandArgument property to add additional information about the command.

Let’s take the example of simple button control.

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

We created a basic submit button in the above code using the asp: Button tag. It works when we click on it when provided with a code in the script file.

Also see, Introduction to C#

Button control properties

Properties

Description

AccessKey sets keyboard shortcuts for the control.
TabIndex sets tab order of the control.
BorderColor sets the border-color of the control..
BorderWidth sets the width of the border of the control.
Width sets the width of the control.
ToolTip displays the text when the mouse hovers on the control.
Font sets the font for the control text.
ForeColor sets the color of the control text.
Height sets the height of the control.
Visible sets visibility of the control.
BackColor sets the background-color of the control.
PostBackUrl THe URL to redirect to a page on clicking the button.

All these prperties help us use the webforms better by enhancing its UI. 
Now let’s learn to write the code to create a working button control.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebControls.aspx.cs"   
Inherits="WebFormsControlls.WebControls" %>  
<!DOCTYPE html>  
<script> 
submit() {
Label1.Text = "your name is" +  name.Text;
}
</script>
<html>  
<head runat="server">   
</head>  
<body>  
    <form id="form1" runat="server">  
        <div>
            <p>NAME</p>
            <asp:TextBox ID="name" runat="server"></asp:TextBox> 
         </div>
         <div>
            <asp:Button ID="Btn" runat="server" Text="Submit" OnClick="submit" />  
        </div>  
    </form>  
    <br />  
    <asp:Label ID="Label1" runat="server"></asp:Label>  
</body>  
</html>  

We created an input box for a name that takes text as input in the above code and created a submit button control. When the user gives input and clicks on the submit button, it displays the user name, as shown below.

FAQs

  1. What is ASP.NET? 
    ASP.NET is an open-source web framework that works on the server-side to produce dynamic web pages using HTML, CSS, JavaScript, TypeScript.
     
  2. What is a button control in ASP.NET? 
    The button control in ASP.NET is a push button that can either be a submit button or a command button. These buttons perform actions on click in a form using the event handlers.
     
  3. What are the different types of button controls? 
    The ASP.NET provides us with three types of button controls. They are:
    Button - a standard button that performs actions on click.
    Link Button - a button that contains a link and redirects the user to the corresponding page.
    Image Button - an image is used as a button.
     
  4. Which property is used to re-direct the user to a page on a button click?
    The PostBackUrl property is used to set the link of a page to redirect the user on the button click.
     
  5. What is the difference between submit button and the command button?
    Both the submit button and command buttons perform actions on click in a form using the event handlers. The submit button does not have a command name, whereas the command button has a command name and can create multiple controls on a page.

Key Takeaways

Let’s discuss this article in brief.

  • ASP.NET is an open-source web framework that works on the server-side to produce dynamic web pages using HTML, CSS, JavaScript, TypeScript.
  • ASP.NET provides us with button controls which are pushbuttons used to perform actions on click.
  • The submit button posts the data and page to the server. It doesn't have a command name, and the event handlers handle the actions on click.
  • The command button allows you to create multiple button controls for a page and has a command name. It also enables you to handle the actions using event handlers.  
     

Apart from this, you can also expand your knowledge by referring to these articles on Features Of ASP Net and ASP Full Form.

Recommended topic: 

What is .Net

Hey Ninjas! Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Happy Learning!

Live masterclass