Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding a Javascript Confirm function on a submit button

Posted on 2006-11-26
8
Medium Priority
?
3,112 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 Ageil earned 1000 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
Independent Software Vendors: 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!

 

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
 

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 Ageil
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 Ageil
ID: 18041884
You welcome. Glad I was of help
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

782 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