DataGrid OnDeleteCommand

R

RN1

A DataGrid, populated with records from a database, has a LinkButton
in the ItemTemplate of a TemplateColumn. I want that when this
LinkButton is clicked, users should first be displayed a JavaScript
confirm dialog. If the user clicks OK in the confirm dialog, then the
record should be deleted but if the user clicks the Cancel button in
the confirm dialog, then that record shouldn't be deleted (i.e.
nothing should happen). This is how I tried it in the OnItemDataBound
event handler of the DataGrid:

--------------------------------------------------------------------------------
Sub Item_DataBound(ByVal obj As Object, ByVal ea As
DataGridItemEventArgs)
If (ea.Item.ItemType = ListItemType.Item Or ea.Item.ItemType =
ListItemType.AlternatingItem) Then
Dim lkb As LinkButton

lkb = CType(ea.Item.FindControl("lkbDelete"), LinkButton)
lkb.Attributes.Add("OnClick", "javascript:return
confirm('Delete Record?')")
End If
End Sub
--------------------------------------------------------------------------------

The above code works fine but if I put the above code in the
OnDeleteCommand event handler of the DataGrid (instead of in the
OnItemDataBound event handler), then the JavaScript confirm dialog
doesn't pop-up. Why?

Moreover, if I comment the "If" condition in the above code (in the
Item_DataBound event handler), then the following error gets
generated:

Object reference not set to an instance of the object.

pointing to the lkb.Attributes.Add line in the above code. What causes
this error?

Thanks,

Ron
 

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

Similar Threads


Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top