3

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.

1
  • Why not use the GridView OnRowCommand event? Commented Jan 11, 2013 at 10:43

3 Answers 3

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I was trying like this: CommandArgument ='<%#Container.DataItemIndex+";"+((Label)(((GridViewRow)Container).Cells[1].Controls[0])).Text+";"+ ((CheckBox)(((GridViewRow) Container).Cells[4].Controls[0])).Checked %>'/> but some error was there: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
2

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

2 Comments

I was trying like this: CommandArgument ='<%#Container.DataItemIndex+";"+((Label)(((GridViewRow)Container).Cells[1].Controls[0])).Text+";"+ ((CheckBox)(((GridViewRow) Container).Cells[4].Controls[0])).Checked %>'/> but some error was there: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
you can't do that ((Label)(((GridViewRow)Container).Cells[1].Cont‌​rols[0])).Text. I think you need to set the command argument in the RowDataBound Event
0

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.

2 Comments

I was trying like this: CommandArgument ='<%#Container.DataItemIndex+";"+((Label)(((GridViewRow)Container).Cells[1].Controls[0])).Text+";"+ ((CheckBox)(((GridViewRow) Container).Cells[4].Controls[0])).Checked %>'/> but some error was there: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
@TamalKantiDey how many columns do you have in your GridViewRow? It seems you are trying to access an out-of-range cell...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.