How to add multiple buttons to a datagrid row... and handle their events

Discussion in 'ASP .Net Datagrid Control' started by AC, Dec 29, 2003.

  1. AC

    AC Guest

    I have a DataTable that I'm currently displaying in a DataGrid... but I have the case where I need to use multiple buttons/linkButtons in each row of the DataGrid (in non-edit mode) so I can manually control the sorting and to remove items from the DataTable.

    Using template columns, I can easily add buttons to one column and assign them unique ID's, but I can't seem to create the event handlers on the server side. It seems that the codebehind can't understand the unique names of the button names (well, they are unique in the column, but when the control is rendered, they'd all have the same name [I know ASP.NET gives them unique IDs] and I'm just using the CommandArgument to pass the unique value back to me).

    In the past, when I've only needed one button, I've just added the Edit button and before the EditCommand method finished, I would just set the EditItemIndex back to -1 so it never got into Edit mode. I knew this was a hack, but it worked and I always meant to revisit this, but never had the time. Well, now I not only have the time, but I'm forced into a corner.

    Granted, I might be using the wrong control... maybe I should be using the DataList. Has anyone had this need? I've sort of seen posts about this when people need to add multiple controls to a templte column when in EditMode, but I am not in edit mode.

    Thanks
    -AC
    AC, Dec 29, 2003
    #1
    1. Advertising

  2. AC

    Teemu Keiski Guest

    Hi,

    you can put Button/LinkButton/ImageButtons to rows and specify them the
    CommandName and CommandArgument attributes. When this kind of control is
    clicked, ItemCommand event of the Datagrid is raised with the given
    arguments (that were specified in the clicked Button). That way you can know
    which button was clicked and you don't have to specify event handlers for
    Click events.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist

    "AC" <> wrote in message
    news:...
    I have a DataTable that I'm currently displaying in a DataGrid... but I have
    the case where I need to use multiple buttons/linkButtons in each row of the
    DataGrid (in non-edit mode) so I can manually control the sorting and to
    remove items from the DataTable.

    Using template columns, I can easily add buttons to one column and assign
    them unique ID's, but I can't seem to create the event handlers on the
    server side. It seems that the codebehind can't understand the unique names
    of the button names (well, they are unique in the column, but when the
    control is rendered, they'd all have the same name [I know ASP.NET gives
    them unique IDs] and I'm just using the CommandArgument to pass the unique
    value back to me).

    In the past, when I've only needed one button, I've just added the Edit
    button and before the EditCommand method finished, I would just set the
    EditItemIndex back to -1 so it never got into Edit mode. I knew this was a
    hack, but it worked and I always meant to revisit this, but never had the
    time. Well, now I not only have the time, but I'm forced into a corner.

    Granted, I might be using the wrong control... maybe I should be using the
    DataList. Has anyone had this need? I've sort of seen posts about this
    when people need to add multiple controls to a templte column when in
    EditMode, but I am not in edit mode.

    Thanks
    -AC
    Teemu Keiski, Jan 1, 2004
    #2
    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. =?Utf-8?B?U3VyZXNo?=
    Replies:
    1
    Views:
    452
    Saravana
    Nov 22, 2004
  2. Replies:
    0
    Views:
    354
  3. Replies:
    0
    Views:
    409
  4. Jeremy Jones

    Datagrid Nav buttons and numeric buttons

    Jeremy Jones, Mar 21, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    603
    Masudur
    Mar 22, 2007
  5. csgraham74

    Add two buttons in datagrid row in same column

    csgraham74, Jun 1, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    166
    Eliyahu Goldin
    Jun 2, 2005
Loading...

Share This Page