Delete Confirmation

  • Thread starter Thread starter Dotnet Guy
  • Start date Start date
D

Dotnet Guy

I want to add a confirmation dialog box before I delete
the records in my form.

I have a piece of javascript code in html that checks for
confirmation. But the main issue lies in calling this
javascript function on pagebehind.

function confirm_delete()
{
if (confirm("Are you sure you want
to delete this item?")==true)
return true;
else
return false;
}

I use btndelete.attributes.add("onclick", "return
confirm_delete();") in the page load event which also
works fine.

But this event fires each time I click the button,
irrespective of whether there are records or not. And I do
not want this to happen. I want this confirmation to pop
up only when the form contains the records.

I also tried putting in an if condition around it like:

if txtID.text<>""
btndelete.attributes.add("onclick", "return confirm_delete
();")
End if

But this works fine only for the first time. Sometimes it
would'nt work also.

Please help.
 
Dotnet Guy said:
I want to add a confirmation dialog box before I delete
the records in my form.

I have a piece of javascript code in html that checks for
confirmation. But the main issue lies in calling this
javascript function on pagebehind.

function confirm_delete()
{
if (confirm("Are you sure you want
to delete this item?")==true)
return true;
else
return false;
}

This is a bit lengthy, you can shorten it to:

function confirm_delete()
{
return confirm("Are you sure you want to delete this item?");
}


Hans Kesting
 
Thats ok. But that does'nt solve my problem. Is there
anyway to fix my problem?

Thanks.
 
Hi Dotnet Guy,



First please change your name that it reflects your real name unless your parents really named you Dotnet Guy in which case I'm sorry ;-) People would like to know with whom they are talking.



Regarding your question try the following:



aspx

<!-- put this piece code in the aspx part of your page -->

<form id="Form1" method="post" runat="server">

<input type="button" value="Delete Item" onserverclick=" btnDeleteItem" onclick="if(!confirm(' Are you sure you want to delete this item?')){return false;}"

runat="server" ID="btnDeleteItem" NAME=" btnDeleteItem ">

</form>



vb

' and add this piece of Code to your code behind page

Public Sub btnDeleteItem(ByVal sender As System.Object, ByVal e As System.EventArgs)

' here is your code doing what ever is needed to delete the item

End Sub



I hope this solves your problem.



Best regards


Daniel Walzenbach

P.S. If you need to contact me simply remove ".NOSPAM" from my email address.
 
I don't know if this is the solution that you are after, but in the
Load event of your page why don't you check to see if any records
exist. If no records exist, why don't you disable your delete button
so it can't be clicked. This should prevent the event from firing as
the button cannot be clicked.

HTH
Dwayne
 
Back
Top