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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="Server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="carid" HeaderText="Card Id" />
<asp:BoundField DataField="Year" HeaderText="year" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnTest" runat="Server" CommandName="Test" Text="Select" CommandArgument='<%#Eval("carid") + ","+Eval("year") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>
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();
GridView1.DataBind();
}
}
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(primaryColumn);
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";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 2;
dr[1] = 2000;
dr[2] = "Honda";
dr[3] = "Civic";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 3;
dr[1] = 2000;
dr[2] = "BMW";
dr[3] = "GM";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 4;
dr[1] = 2000;
dr[2] = "Swift";
dr[3] = "Tata";
data.Rows.Add(dr);
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];
}
}
}