How to pass multiple values through the Command Argument

In this post, I’ll show you how to pass multiple values in the CommandArgument by using a simple example. It’s actually really easy to do. The CommandArgument property gets or sets an optional parameter passed to the command event handler. So you just have to create one string value with all your desired values that you want to pass seperated by any specific char as value for the CommandArgument. Then you have to split the CommandArgument value in the code behind page to retrieve all the separated values.

In my example I’ll show you how to pass multiple values from a gridview item, through the CommandArgument of the LinkButton.


ASPX code:

<asp:GridView ID="PersonsGrid" runat="server" AutoGenerateColumns="false" >
  <asp:BoundField DataField="Id" HeaderText="Id" />         
  <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
  <asp:BoundField DataField="LastName" HeaderText="LastName" />
  <asp:BoundField DataField="EmailAdress" HeaderText="EmailAdress" />
  <asp:BoundField DataField="Gender" HeaderText="Gender" />
  <asp:BoundField DataField="Birthdate" HeaderText="Birthdate" />
      <asp:LinkButton ID="lnkClickMe" OnClick="lnkClickMe_Click" 
      runat="server" Text="Click Me" CommandArgument='
	  <%# Eval("Id") + ";" + Eval("FirstName") + ";" + Eval("LastName") %>' />

CS code:

protected void lnkClickMe_Click(object sender, EventArgs e)
	LinkButton b = (LinkButton)sender;

	string arguments = b.CommandArgument;
	string[] args = arguments.Split(';');
	string id = args[0];
	string firstname = args[1];
	string lastname = args[2];

Comments are closed.

%d bloggers like this: