Solved

Adding a Javascript Confirm function on a submit button

Posted on 2006-11-26
8
3,074 Views
Last Modified: 2011-10-03
I have a page in my application that has required fields on it. The required field validator work fine (client side) until I put the confirm function on the "onclientclick" attribute of the linkbutton.

when I click the submit link button with the js confirm script on the button, the confirm pops up but the validators do not show and if you click ok on the pop up the form submits even if the required field is empty.

linkbutton looks like this.
<asp:linkbutton id="lnkBtnSubmit" .... onclientclick="return confirmAction("Are you sure you want to submit this form. Click OK to proceed.")" onclick="lnkBtnSubmit_Click" />

Javascript function:
<script>
 
function confirmAction(message){

  return confirm(message);

}
</script>
0
Comment
Question by:scaldwe
8 Comments
 
LVL 27

Accepted Solution

by:
Sammy earned 250 total points
ID: 18017377
try this in your page load

lnkBtnSubmit.Attributes.Add("onclick","javascript:confirmAction('Are you sure you want to submit this form. Click OK to proceed.');");


one more option to try is removing "return" and using
<asp:linkbutton id="lnkBtnSubmit" .... onclientclick="confirmAction("Are you sure you want to submit this form. Click OK to proceed.");" onclick="lnkBtnSubmit_Click" />

HTH
0
 
LVL 10

Expert Comment

by:orbulat
ID: 18017378
u can try in code behind

lnkBtnSubmit.Attributes.Add("onClick"," return confirmAction('Are you sure you want to submit this form. Click OK to proceed.');");
0
 
LVL 4

Expert Comment

by:satish_nagdev
ID: 18017411
hi,
could you try this please

<asp:linkbutton id="lnkBtnSubmit" .... onclientclick="if (confirmAction("Are you sure you want to submit this form. Click OK to proceed.")==false return false;" onclick="lnkBtnSubmit_Click" />

regards,
satish.
0
 

Author Comment

by:scaldwe
ID: 18019771
Thanks Sammy and satish .. both work great, sammy was just first for the points.

It seems the return statement was weirdly causing the validators not to activate or something.

thanks again.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:scaldwe
ID: 18021417
Ok .. a new issua has arose ...

By taking out the return in the above solutions, the validators work and keep the form from submitting when they click OK. On the other hand if the cancel button is clicked on the confirm pop up, The validators still work and will not submit the form if the page is invalid, but if the page's required fields are all filled in( page is valid) it still submits the form.

Any Ideas
0
 
LVL 27

Expert Comment

by:Sammy
ID: 18035193
This works

<script type="text/javascript">
function confirmAction(message){
  if (!confirm(message))
  {
   return false ;  
   }
  else {
   return Page_ClientValidate();  //included in the framework and added whenever you use any validation controls
    }
  }
    </script>

your code behind should be
lnkBtnSubmit.Attributes.Add("onclick" , "return confirmAction('Are you sure you want to submit this form. Click OK to proceed.');");
this way if the user clicks on OK or Cancel and the validation is not met, postback will not happen and if they click Ok they have to meet the validation rules or the page will not postback

0
 

Author Comment

by:scaldwe
ID: 18037466
Sammy friggin rules!!! Thanks for the input. It works perfecto
0
 
LVL 27

Expert Comment

by:Sammy
ID: 18041884
You welcome. Glad I was of help
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now