Tamal Kanti Dey
Tamal Kanti Dey

Reputation: 576

How to pass multiple arguments in CommandArgument in GridView?

I was working on asp.net GridView control. Now I need to edit some row data. For that I was using this code:

 <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="QuickEdit"  OnClick="btnEdit_Click"
  CommandArgument ='<%# ((CheckBox)(((GridViewRow) Container).Cells[4].Controls[0])).Checked %>'/>

And the btnEdit_Click method is:

protected void btnEdit_Click(object sender,EventArgs  e)
{
    LinkButton btn = (LinkButton)sender;
    switch (btn.CommandName)
    {
        case "QuickEdit":

        EditPanel.Visible = true;
        GridPanel.Visible = false;
        CheckBox cbRequiresState = (CheckBox)EditPanel.FindControl("checkRequiresState");

        if (btn.CommandArgument =="True")
        {
            cbRequiresState.Checked = true;
        }
        else
        {
            cbRequiresState.Checked = false;
        }

        break;
    }
}

Now, I need to pass more than one argument as CommandArgument to that btnEdit_Click method. For that what I need to do? And please suggest me a good way to utilize those arguments in that method.

Upvotes: 3

Views: 14663

Answers (3)

gwt
gwt

Reputation: 2413

here is an example :

in your aspx code :

<asp:ImageButton ID="btnSelect" runat="server" ImageUrl="~/Images/btnSelect.png" CommandName="Select" CommandArgument='<%# Container.DataItemIndex +";"+Eval("ID") %>' ToolTip="select" CausesValidation="False" /></ItemTemplate>

and in your code behind :

    string info = e.CommandArgument.ToString();
    string[] arg = new string[2];
    char[] splitter = { ';' };
    arg = info.Split(splitter);

Upvotes: 5

davioooh
davioooh

Reputation: 24676

Because CommandArgument is a simple string, concatenate the arguments you want to pass to the event putting some kind of separator among them.

Then in btnEdit_Click split the values by separator.

NOTE: Chose the sepatator so that it isn't a character contained in anyone of the parameters passed to the event.

Upvotes: 0

GeorgesD
GeorgesD

Reputation: 1082

You can use a string and separate the values by ; or another character.

Upvotes: 2

Related Questions