[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Displaying "Are you sure?" alert before firing a server-side button

Posted on 2004-09-14
13
Medium Priority
?
285 Views
Last Modified: 2012-08-13
I have a "Delete Record" button. I want to give the user a chance to confirm his intentions before deleting the record, and would like to display a message box with "Are you sure?" and OK/Cancel. If the user presses OK the delete button posts back to the server, otherwise no further action is taken.

I know you can use the onclick event in hyperlinks, returning 'false' prevents the redirection. How do I do it with a server-side ASP.NET button?

I tried putting the following in the PageLoad event, but it didn't even display the message:

    cmdDelete.Attributes("onclick") = "javascript:msgbox('Delete this record?')"
0
Comment
Question by:crescendo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 1000 total points
ID: 12054021
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12054026
0
 
LVL 35

Expert Comment

by:YZlat
ID: 12054192
if (confirm("'Delete this record?")) {alert("Deleting..")}
else{exit;};
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:YZlat
ID: 12054233
try this:

cmdDelete.Attributes("onclick") = "javascript:if (confirm('Delete this record?')) {alert('Deleting..')}else{exit;};"
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12054246
assuming ur button name is "butDelete"
.vb file
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        butDelete.Attributes.Add("Language", "javascript")
        butDelete.Attributes.Add("onclick", "return CheckDelete()")
End Sub

.aspx file
<script language="vbscript" id="Scriptfunction">
function CheckDelete()
   if msgbox("Are you sure you want to delete this detail?", vbquestion + vbyesno,"MFD") = vbno then
      CheckDelete = false
   end if
end function
</script>
0
 
LVL 9

Author Comment

by:crescendo
ID: 12054256
Baan:

I tried both ways, but the message box is not being fired. Any ideas? Here is my code:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        lblMsg.Text = " "

        cmdDelete.Attributes.Add("onclick", "javascript:return confirm('Delete this record?')")

        If Not Page.IsPostBack Then
0
 
LVL 9

Author Comment

by:crescendo
ID: 12054271
I noticed that in the articles, the button is a LinkButton, mine is a straight ASP.NET button control. Does that make a difference?
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12054306
have u tried what i suggested?
0
 
LVL 9

Author Comment

by:crescendo
ID: 12054380
Baan

I solved it. I had "CausesValidation = True" on the button. That was inserting its own javascript into mine, and screwing it up. Works fine now.

Thanks to all.

0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12054414
1. Add the following script in your .aspx page:
<script language="javascript">
function ConfirmDelete()
{
   var deleteRecord = confirm("Are you sure you want to delete the selected record?");
  if (deleteRecord == false)
  {
    return false;
  }
}
</script>


2. Add the following attribute to the link button:


linkButton.Attributes.Add("OnClick","javascript: return ConfirmDelete();");

HTH, Nauman.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12054427
That shouldn't make any difference. Put a ";" at the end..
cmdDelete.Attributes.Add("onclick", "javascript:return confirm('Delete this record?');")
                                                                                                                    ~~~~  here
0
 
LVL 9

Author Comment

by:crescendo
ID: 12054775
Hi

Yes, I can see that a semi-colon would help, however, the code that comes next is the test for client-side validation. If I needed to validate the fields before proceeding with the delete (which I don't) then it should validate *before* it asks the user. However, it's a non-issue as I'm not concerned in this case whether the user has entered any other data.

It's an interesting point, though. I can foresee many circumstances where you might want to enter data and then prompt before processing it.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12054891
>>I had "CausesValidation = True" on the button
oh! so u had some validation controls set in the page? if s, then it would create the onlick event for the button to call the validators ...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question