How to add a button with custom command name and perform an operation?

Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 27442 red flag
Rating: 5 out of 5  
 1 vote(s)

To add a button for each record in the GridView and perform some custom operation on click of this
button, we can follow this approach.

GridView control is a powerful data grid control that allows us to display the data in tabular format with facilities for sorting and pagination. It also allows us to manipulate the data.


In the previous article we learnt about How to generate edit, delete, update and select buttons automatically? In this article we shall learn how to add a button for each record in the GridView and perform some custom operation on click of this button. In order to do that we can follow this approach.


<asp:GridView ID="GridView1" runat="server" OnRowCommand="FireRowCommand" DataKeyNames="AutoId">


<asp:TemplateField HeaderText="Mark as Deleted">


<asp:LinkButton ID="btnDeleted" runat="server" CommandArgument='<%# Eval("AutoId") %>'

CommandName="MarkAsDeleted" Text="Mark As Deleted" />



<asp:TemplateField HeaderText="Mark as Archived">


<asp:LinkButton ID="btnArchived" runat="server" CommandArgument='<%# Eval("AutoId") %>'

CommandName="MarkAsArchived" Text="Mark as Archived" />






In the above code snippet, we have a GridView where we have kept two TemplateField with LinkButtons. The first button CommandName is “MarkAsDeleted” and the second button CommandName is “MarkAsArchived”.


string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;


protected void Page_Load(object sender, EventArgs e)


if (!IsPostBack)






private void GetData()


DataTable table = new DataTable();

// get the connection

using (SqlConnection conn = new SqlConnection(_connStr))


// write the sql statement to execute

string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail ORDER By AutoId";

// instantiate the command object to fire

using (SqlCommand cmd = new SqlCommand(sql, conn))


// get the adapter object and attach the command object to it

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))


// fire Fill method to fetch the data and fill into DataTable





// specify the data source for the GridView

GridView1.DataSource = table;

// bind the data now




protected void FireRowCommand(object sender, GridViewCommandEventArgs e)


string command = e.CommandName;

string autoId = e.CommandArgument.ToString();

switch (command)


case "MarkAsDeleted":

Response.Write("<b>Mark As Deleted for " + autoId + " </b> button clicked");


case "MarkAsArchived":

Response.Write("<b>Mark As Archived for " + autoId + " </b> button clicked");





When these "MarkAsArchived",“MarkAsDeleted” buttons are clicked the OnRowCommand event fires that execute the FireRowCommand sever side method. In this method, we have retrieved the CommandName and CommandArguments that we had set for buttons in the GridView. The CommandName differs from button to button and based on its value we have written text under the switch statement.


Note: The same can be achived using normal button as well instead of LinkButton.

Thanks for reading, hope you liked it.

Keep reading my forth coming articles. To read my series of articles on ASP.NET, click here.
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Posted by: Ashwink2 on: 1/18/2019 | Points: 25
I like the way you represent your ideas and this useful answer for those who are working with ASP.NET am also using windows 10 and looking for the complete knowledge and advantages of command prompt.

Login to post response

Comment using Facebook(Author doesn't get notification)