Changea buttoncommand for one record only

R

Rock

I have a datagrid with the standard Edit and Delete columns that you can add
using the property builder. I added a 3rd column for "Confirm Delete".

When the Delete button is clicked, it runs dg_ItemCommand, and changes the
visible column to say "Confirm Delete". If user clicks on ConfirmDelete, it
then runs the handler dg_DeleteCommand and deletes the record.

The problem is that the column changes for every record so that every record
displays "Confirm Delete", and I'd rather display it only on the record the
user has clicked on.

Does anyone know how to do this? My code is below.

Private Sub dg_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.ItemCommand

If (e.CommandName = "ConfirmDelete") Then
dg.Columns(1).Visible = False
dg.Columns(2).Visible = True
End If

end sub


Private Sub dg_DeleteCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.DeleteCommand

Dim strGuid As String
strGuid = e.Item.Cells(1).Text
Call DeleteRecord(strGuid, strTableName)
dg.Columns(1).Visible = True
dg.Columns(2).Visible = False

End Sub


<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Edit" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Delete"
CommandName="ConfirmDelete"></asp:ButtonColumn>
<asp:ButtonColumn Text="Confirm Delete" HeaderText="Confirm Delete"
CommandName="Delete" Visible=false></asp:ButtonColumn>
<asp:ButtonColumn Text="Copy" CommandName="Copy"></asp:ButtonColumn>
<asp:BoundColumn Visible="False" DataField="ID"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Yr" HeaderText="Year"></asp:BoundColumn>
 
M

Mike Ryan

Hello,

I don't think you'll get the results you're looking for with this
approach--setting the visibility would only work if you could hide rows
selectively and somehow merge the columns visually.

Here's a solution: keep what you have below, but delete the "Delete"
column. Then change your ItemCommand event to the following (hope this
works, haven't done VB in a while):

If (e.CommandName = "ConfirmDelete") Then
Dim lbtn As LinkButton
lbtn = CType(e.Item.Cells(1).Controls(0), LinkButton)
lbtn.Text = "Confirm Delete"
lbtn.CommandName = "Delete"
End If

What happens is the first time the link is clicked, the ItemCommand
event fires and changes the command from ConfirmDelete to Delete for
that link only. Clicking the link again then fires your Delete event and
you can do your bidness.

So don't create two columns--instead use one and change the function of
the selected link (in your Delete function you probably want to change
the CommandName back to "ConfirmDelete").

- Mike
 

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,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top