Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple argument through CommandArgument of Button in Asp.net

I have a gridview with multiple rows, each has a Update button and I need to pass 2 values when someone clicks on Update button. Aside from packing the arguments inside CommandArgument separated by commas (archaic and not elegant), how would I pass more than one argument?

<asp:LinkButton ID="UpdateButton" runat="server" CommandName="UpdateRow" CommandArgument="arg_value" Text="Update and Insert" OnCommand="CommandButton_Click" ></asp:LinkButton> 

As a note, the values can't be retrieved from any controls on the page, so I am presently not seeking any design solutions.

like image 848
sarsnake Avatar asked Mar 05 '10 19:03

sarsnake


People also ask

How to pass multiple values in CommandArgument in ASP 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.

What is command argument in asp net c#?

The CommandArgument property complements the CommandName property by allowing you to provide any additional information about the command to perform. For example, you can set the CommandName property to Sort and set the CommandArgument property to Ascending to specify a command to sort in ascending order.


2 Answers

You can pass semicolon separated values as command argument and then split the string and use it.

<asp:TemplateField ShowHeader="false">     <ItemTemplate>        <asp:LinkButton ID="lnkCustomize" Text="Customize"  CommandName="Customize"  CommandArgument='<%#Eval("IdTemplate") + ";" +Eval("EntityId")%>'  runat="server">         </asp:LinkButton>        </ItemTemplate>    </asp:TemplateField> 

at server side

protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e) {       string[] arg = new string[2];       arg = e.CommandArgument.ToString().Split(';');       Session["IdTemplate"] = arg[0];       Session["IdEntity"] = arg[1];       Response.Redirect("Samplepage.aspx"); } 

Hope it helps!!!!

like image 148
Archana Motagi Avatar answered Oct 05 '22 08:10

Archana Motagi


After poking around it looks like Kelsey is correct.

Just use a comma or something and split it when you want to consume it.

like image 43
cazlab Avatar answered Oct 05 '22 08:10

cazlab