GridView with 2 buttons. Which one was clicked

Discussion in 'ASP .Net' started by Yossi, Nov 17, 2006.

  1. Yossi

    Yossi Guest

    I have a grid view with several text columns and 2 button columns. How do I
    tell which button on which row was clicked?
     
    Yossi, Nov 17, 2006
    #1
    1. Advertising

  2. Yossi

    Onwuka Emeka Guest

    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;

    }


    }


    "Yossi" <> wrote in message
    news:...
    >I have a grid view with several text columns and 2 button columns. How do
    >I tell which button on which row was clicked?
    >
     
    Onwuka Emeka, Nov 17, 2006
    #2
    1. Advertising

  3. Yossi

    Yossi Guest

    That solves half of the problem.

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


    "Onwuka Emeka" <> wrote in message
    news:%...
    > 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;
    >
    > }
    >
    >
    > }
    >
    >
    > "Yossi" <> wrote in message
    > news:...
    >>I have a grid view with several text columns and 2 button columns. How do
    >>I tell which button on which row was clicked?
    >>

    >
    >
     
    Yossi, Nov 17, 2006
    #3
  4. Yossi

    Mark Rae Guest

    "Yossi" <> wrote in message
    news:...

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


    Set the CommandArgument to the row's unique key.
     
    Mark Rae, Nov 17, 2006
    #4
  5. Yossi

    Yossi Guest

    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.

    "Mark Rae" <> wrote in message
    news:OIvC3%...
    > "Yossi" <> wrote in message
    > news:...
    >
    >> Now how do I determine on which row that button is located?

    >
    > Set the CommandArgument to the row's unique key.
    >
     
    Yossi, Nov 17, 2006
    #5
  6. Yossi

    Yossi Guest

    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.


    "Mark Rae" <> wrote in message
    news:OIvC3%...
    > "Yossi" <> wrote in message
    > news:...
    >
    >> Now how do I determine on which row that button is located?

    >
    > Set the CommandArgument to the row's unique key.
    >
     
    Yossi, Nov 17, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger
    Replies:
    1
    Views:
    640
    GreggTB
    May 20, 2005
  2. bthumber
    Replies:
    4
    Views:
    479
    bthumber
    Jun 25, 2010
  3. Yossi

    Which button was clicked on GridView

    Yossi, Nov 17, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    702
    Yossi
    Nov 17, 2006
  4. Yossi

    Which button was clicked and on which row?

    Yossi, Nov 17, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    739
    Yossi
    Nov 17, 2006
  5. Mrinalini Pande

    Find which Cell is clicked by user in GridView control of Asp.net

    Mrinalini Pande, Nov 23, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    762
    Mrinalini Pande
    Nov 23, 2006
Loading...

Share This Page