suppressing Submit command with a Delete button

  • Thread starter Elliot M. Rodriguez
  • Start date
E

Elliot M. Rodriguez

I have a Delete button in my datagrid. I'm attempting to add some code
confirming whether the user wishes to really delete the item or not.

The confirm pops up, but I cannot suppress the submission when selecting
Cancel.

Heres what I have so far:

' adding the javascript call to the button:
Private Sub dgItem_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgItem.ItemCreated

If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then

Dim objDeleteButton As Button = CType(e.Item.Cells(1).Controls(0), Button)

objDeleteButton.CssClass = "inputbutton"

objDeleteButton.Attributes.Add("onClick", "confirmDeleteItem();")

End If

End Sub



' client side code in javascript

function confirmDeleteItem()

{

if (confirm("Are you sure you wish to delete this item?"))

{

return true;

}

else

{

return false;

}

}

any help is appreciated. thank you.
 
C

Chris Jackson

Sounds like you have validation controls on there, which causes the runtime
to emit yet another onClick handler which submits the form. Try using an
<input type="button"> instead of an <asp:button> control, and if you need
validation just script in the validation call (and, as always, make sure you
explicitly validate on the server, as client side validation is easy to
foil).
 
E

Elliot M. Rodriguez

Chris:

Thanks for the reply. I am struggling with this.

If I take your approach it seems I dont have the ability to wire it to the
DeleteCommand event. This means I have to access the DataKey for the row
through the client, which I dont want to do. I could be wrong on this
though.

All I really want to do is have a DeleteCommand button that performs a
delete with a confirmation prior to deletion. My code does not have any
other validators on the page. There is another button on the page, but
performs a completely unrelated task.
 
E

Elliot M. Rodriguez

Chris:

I realized I forgot the lousy "return" keyword in Attributes.Add lol...
Thanks for the article and the help.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top