GridView with 2 buttons. Which one was clicked

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?
 
O

Onwuka Emeka

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;

}


}
 
Y

Yossi

That solves half of the problem.

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

Yossi

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.
 
Y

Yossi

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top