GridView with 2 buttons. Which one was clicked

  • Thread starter Thread starter Yossi
  • Start date Start date
Y

Yossi

I have a grid view with several text columns and 2 button columns. How do I
tell which button on which row was clicked?
 
Hi Yossi,

Set the CommandName property of the two buttons eg button1.CommandName =
"This" button2.CommandName = "That"
handle the RowCommand of the GridView and switch through the commandnames

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs
e)

{

switch (e.CommandName)

{

case "This":

Response.Write("This was clicked");

break;

case "That":

Response.Write("That was clicked");

break;

}


}
 
That solves half of the problem.

Now how do I determine on which row that button is located?
 
How do you do that?

I am talking here about the GridView component in ASP .Net 2.0. When I look
at the source for the GridView, I see two <asp:ButtonField....> elements.
If I add a CommandArgument attribute, I get an error. It seems that
ButtonFields do not have a CommandArgument attribute.
 
I found the answer in the MSDN:

To determine the index of the record that raises the command event, use the
CommandArgument property of the event argument that is passed to the command
event for the data-bound control. The ButtonField class automatically
populates the CommandArgument property with the appropriate index value.

Intellisense was not showing me a CommandArgument. That is why I got
confused. Now all I have to do is set different CommandNames for each
button in order to determine which one was clicked. Before, I set the
CommandName of both buttons to Select and then I was handling the
OnSelectedIndexChanged event. That was giving me the row, but not which
button fired the select.
 
Back
Top