In this article I will explain with an example, how to pass (send) multiple values in CommandArgument property on Button Click in ASP.Net GridView using C# and VB.Net.
Multiple values will be passed by concatenating multiple values using a Character separator such as Comma, Pipe, etc. and later inside the Button Click event, the values will be split and populated into an Array.
HTML Markup
The following HTML Markup consists of an ASP.Net GridView control with three BoundField columns and a TemplateField column.
The TemplateField column consists of a Button control specified with Click event handler and a CommandArgument property.
Multiple field values are concatenated using Comma character as separator and set into the CommandArgument property.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelect" runat="server" Text="Select" OnClick="Row_Select" CommandArgument='<%# Eval("Id") + "," + Eval("Name") + "," + Eval("Country") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net GridView control
I have created a dynamic DataTable with some dummy data and it is used to populate the GridView control in the Page Load event.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Pass (Send) multiple values in CommandArgument on Button Click
Inside the Button Click event handler, first the Button is referenced and then the CommandArgument property is fetched.
The multiple values in CommandArgument property are separated by splitting into a String Array.
C#
protected void Row_Select(object sender, EventArgs e)
{
Button btnSelect = (sender as Button);
string[] commandArguments = btnSelect.CommandArgument.Split(',');
}
VB.Net
Protected Sub Row_Select(ByVal sender As Object, ByVal e As EventArgs)
Dim btnSelect As Button = CType(sender, Button)
Dim commandArguments() As String = btnSelect.CommandArgument.Split(",")
End Sub
Screenshot
Downloads