How to pass more than one command argument in gridview

As per the Microsoft,ASP.NET is:-

ASP.NET is an open-source, server-side web application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, applications and services.

In this post, I will show you that how you can pass the multiple values CommandArgument from GridView to the code-behind. The solution is very simple Just use a comma or something and split it when you want to consume it.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultipleCommandArgument.aspx.cs"
   Inherits="MultipleCommandArgument" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
   <title>Untitled Page</title>
   <form id="form1" runat="server">
           <asp:GridView ID="GridView1" runat="Server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
                   <asp:BoundField DataField="carid" HeaderText="Card Id" />
                   <asp:BoundField DataField="Year" HeaderText="year" />
                           <asp:Button ID="btnTest" runat="Server" CommandName="Test" Text="Select" CommandArgument='<%#Eval("carid") + ","+Eval("year") %>' />
           <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
           <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MultipleCommandArgument : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

            GridView1.DataSource = GetData();

    DataTable data;
    DataTable GetData()
        data = Session["data"] as DataTable;
        if (data != null)
            return data;
        data = new DataTable();
        DataColumn primaryColumn
        = new DataColumn("carid", typeof(Int32));
        data.Columns.Add(new DataColumn("year", typeof(Int32)));
        data.Columns.Add(new DataColumn("make", typeof(string)));
        data.Columns.Add(new DataColumn("model", typeof(string)));
        DataRow dr;
        dr = data.NewRow();
        dr[0] = 1;
        dr[1] = 1998;
        dr[2] = "Isuzu";
        dr[3] = "Trooper";
        dr = data.NewRow();
        dr[0] = 2;
        dr[1] = 2000;
        dr[2] = "Honda";
        dr[3] = "Civic";
        dr = data.NewRow();
        dr[0] = 3;
        dr[1] = 2000;
        dr[2] = "BMW";
        dr[3] = "GM";
        dr = data.NewRow();
        dr[0] = 4;
        dr[1] = 2000;
        dr[2] = "Swift";
        dr[3] = "Tata";
        DataColumn[] primaryColumns = new DataColumn[1];
        primaryColumns[0] = primaryColumn;
        data.PrimaryKey = primaryColumns;
        Session["data"] = data;
        return data;

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        if (e.CommandName == "Test")
            string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });

       Label1.Text= commandArgs[0];
       Label2.Text = commandArgs[1];

Next Post Previous Post
No Comment
Add Comment
comment url