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

A

AC

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
 
T

Teemu Keiski

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

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top