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
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