views:

1198

answers:

4

I have an ASP.NET GridView control with two asp:CommandField columns that are both using the Select command to perform different tasks. How do I distinguish which column was selected in the OnRowCommand event when both return "Select" when I check the CommandName property of the GridViewCommandEventArgs object?

Here is my source code:

ASPX page:

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false" OnRowCommand="MyGridView_OnRowCommand">
    <Columns>
        <asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="Click Me!" />
        <asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="No Click Me!" />
    </Columns>
</asp:GridView>

Code behind:

protected void MyGridView_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    string x = e.CommandName //returns "Select" for both asp:CommandField columns
}
A: 

Use the GridViewCommandEventArgs.CommandArgument property !

Cerebrus
the CommandArgument property return the index of which row was selected. I need to know which asp:commandfield was selected within the row.
Michael Kniskern
No, the CommandArgument returns whatever argument you specify. So you can have buttons with the same CommandName, but different Arguments. This helps you identify which button raised the command.
Cerebrus
+2  A: 

Use a button column instead that way you can specify the specific command names and work with it from there

<asp:ButtonField ButtonType="Link" Text="Click Me" CommandName="MyCommand1" />
<asp:ButtonField ButtonType="Link" Text="No Click Me" CommandName="MyCommand2" />

Then you can take action based on e.CommandName

Mitchel Sellers
I tried the CommandName property and got the following error: Type 'System.Web.UI.WebControls.CommandField' does not have a public property named 'CommandName'
Michael Kniskern
you have to create it dynamically to do this.
Eric
look at the answer i gave above.
Eric
@Michael - Make sure you are using my updated example It is a ButtonColumn
Mitchel Sellers
@Eric YOu do not have to create anything dynamically to use it, you just have to use a ButtonColumn instead.
Mitchel Sellers
@Mitch - Still does not work. I get the following compilation error: Cannot create an object of type 'System.Web.UI.WebControls.ButtonType' from its string representation 'LinkButton' for the 'ButtonType' property.
Michael Kniskern
@Michael - Oops, I had the syntax down for a DataGrid, I updated the example with the proper value for a GridView.
Mitchel Sellers
@Mitch - the example with produce the first compilation error mentioned in my first comment
Michael Kniskern
@Michael - No, if you copy/paste my example it works as is. Your first error was from a different script that I had. I just re-edited the post, that has been pulled from a working test app.
Mitchel Sellers
Thanks, I switch from an asp:CommandField column to an asp:ButtonField column
Michael Kniskern
Sorry for the confusion with the early posts!
Mitchel Sellers
A: 

Well, first do you HAVE to use SELECt as the command? You could set that to something else that makes more sense.

Secondly, you could set the CommandArgument property to different values so you know which one is being clicked.

Telos
Well, it makes sense to use the Select command because I am not performing any CRUD operations on the record. Both scenarios just require an informational query.
Michael Kniskern
But a command could be anything. It could be select_one or select_two for instance...
Telos
The whole purpose of my question is trying to determine if there is a way to indicate that select_one was clicked.
Michael Kniskern
Yes, and you can set the command name to select_one. Or you could set the CommandArgument property to One. Either way you can check to see which command was clicked...
Telos
A: 

use the command argument property.

e.commandargument

Or you can dynamically create the button in the command field and set the commandName to anything you want.

in gridview_Load

for (int i = 0; i <= GridView1.Rows.Count - 1; i++) {

    linkbutton btnedit = new linkbutton();


    GridView1.Rows(i).Cells(3).Controls.Add(btnedit);
   //the above is where you want the button to be on the grid
    btndel.CommandName = "Select2";
    btndel.CommandArgument = "whatever you want";
}

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
    If e.CommandName = "Select1" Then 

       //do stuff
End Sub
Eric
The manual addition of the link is overkill in this users situation, and requires a secondary traversal of the grid items which isn't needed.
Mitchel Sellers