Example of Button Control in ASP.NET


In this post on the Example of Button Control in ASP.NET, I will explain the Button control using few examples.

The Button control in ASP.NET displays a push button on a Web Form. Basically, we use the Button control to post a page on the web server. Since, a postback submits a web page to the server for further processing, it is required to provide the code that should execute whenever postback occurs.

Hence, it is advisable to provide an event handler associated with the click event that executes when a user clicks the button. Additionally, the Button control has a CommandName property which is not available with the Button by default. Therefore, the user needs to specify it explicitly.

In the case of several buttons present on the Web Form, the CommandName property determines which of these buttons are clicked. While the CommandArgument property provides additional information. Also, we can provide an event handler for the Comand Event. The following example demonstrates the use of the Button control along with CommandName property and Command event.

In the following Example of Button Control in ASP.NET, we create three Command Buttons, that display a specific Bulleted List control dynamically. Further, the example shows how to use Attributes collection to associate a CSS class name with a DIV tag dynamically through the code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ButtonControlExample.WebForm1" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
            background-color: bisque;
            min-height: 100px;
            margin: 50px;
            padding: 50px;
            font-size: 20px;
            font-weight: bold;
            border-radius: 10px;
            width: 500px;
            background-color: darkred;
    <form id="form1" runat="server">
        <div id="d1" runat="server">
            <asp:Button ID="Button1" runat="server" Text="Show Color List" 
                OnCommand="Button1_Click" CommandName="Color" />
            <asp:Button ID="Button2" runat="server" Text="Show Course List" 
                OnCommand="Button1_Click" CommandName="Course" />
            <asp:Button ID="Button3" runat="server" Text="Show Fruit List" 
                OnCommand="Button1_Click" CommandName="Fruit" />

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ButtonControlExample
    public partial class WebForm1 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            d1.Attributes.Add("class", "c1");
            Button1.Attributes.Add("class", "c2");
            Button2.Attributes.Add("class", "c2");
            Button3.Attributes.Add("class", "c2");

        public void DisplayColorList() {
            BulletedList b1 = new BulletedList();
        public void DisplayFruitList()
            BulletedList b1 = new BulletedList();


        public void DisplayCourseList()
            BulletedList b1 = new BulletedList();


        protected void Button1_Click(object sender, CommandEventArgs e)
            String str = e.CommandName;
            if (str.Equals("Fruit"))
            if (str.Equals("Course"))


Use of CommandName Property of Button Control
Use of CommandName Property of Button Control

Further Reading

Parameter and ParameterCollection in ADO.NET

Database Manipulation Using DataGrid

Example of Button and Link Button Control in ASP.NET

Example of Chart Control in ASP.NET

Creating a DataTable from a DataReader in ASP.NET

Deleting a Record using DataGrid Control in ASP.NET

Edit a Record Using DataGrid Control in ASP.NET

Insert a Record Using ItemCommand Event in DataGrid

CRUD Operations with DataGrid in ASP.NET

Creating Columns in a DataGrid Control

XML Documents and DataSet in ASP.NET

Code Render Block in ASP.NET

ASP.NET Core Features and Advantages

Display Images Using DataList Control

Adding Images Using Image Control

Creating a Group of Radio Buttons Using RadioButtonList Control

Example of Button Control in ASP.NET

Using MD5 Hash Algorithm

ItemDataBound Event in DataList

More Features of DataList in ASP.NET

A Simple Example of Using a DataList Control in ASP.NET

Properties and Methods of DataList Control in ASP.NET

ASP.NET Practice Exercise

Exploring DataList Control in ASP.NET

Custom Validator Control in ASP.NET

Validation Summary Control in ASP.NET

Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator

An Example of Data Binding with RadioButtonList Control

Binding Data to Web Control in ADO.NET

Examples of AdRotator Control in ASP.NET

Examples of Validation Controls in ASP.NET

Overview of MVC architecture in ASP.NET


You may also like...