3

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
}   
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231

5 Answers5

4

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
  • 62,228
  • 14
  • 110
  • 173
0

The answer you seek is simple and tricky. I had that problem too in my website project, so I surfed the internet for days and not found what I and you were needed.

One day I just thought about the problem alone and made experiments for hours and finally realized that the only easy way to know the column that the button was clicked is in the RowCommand event of the GridView in the CommandName property of the GridViewCommandEventArgs. More correctly probably and comfortable is to edit columns of GridView through the design mode and replace your Command fields to Button fields.

You can give any string/text you want to each button field in its CommandName, so in the RowCommand event you can know which was clicked. Button1 or Button2, but you don't want to give these strings, because you request something that the buttons should select and give to you, so the CommandName should be the word select, but it can be SELECT too and Select and selecT and etc.

The select command can be mention in the CommandName in many forms, and still the system will recognize it. So for example if you have two Button fields in the GridView, whose first CommandName is simply select and the other is SELECT, then when any of them is clicked the RowCommand event raises and

if (e.CommandName == "select")
  { 
   Label1.Text = "You clicked first button";
  }
  else 
  {
   Label1.Text = "You clicked second button";
  }

and the SelectedDataKey of your GridView will contains the data you requested. What other people offered in their answer to differ in CommandName by setting button one to select_one and setting button two to select_two will help you to know if either select_one was clicked or other, but the SelectedDataKey of your GridView will remain null or DataKey instance that doesn't contain the information you need at all.

In other words, the buttons will not select any necessary information, so it is very important to follow my answer. It is the exact, perfect and great solution to your problem!

Community
  • 1
  • 1
0

Use the GridViewCommandEventArgs.CommandArgument property !

Cerebrus
  • 25,615
  • 8
  • 56
  • 70
  • 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 Jun 03 '09 at 17:51
  • 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 Jun 04 '09 at 05:55
0

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.

CodeRedick
  • 7,346
  • 7
  • 46
  • 72
  • 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 Jun 03 '09 at 17:47
  • But a command could be anything. It could be select_one or select_two for instance... – CodeRedick Jun 03 '09 at 17:54
  • The whole purpose of my question is trying to determine if there is a way to indicate that select_one was clicked. – Michael Kniskern Jun 03 '09 at 18:12
  • 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... – CodeRedick Jun 03 '09 at 18:52
0

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
  • 7,930
  • 17
  • 96
  • 128
  • 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 Jun 03 '09 at 17:53