Solved

Adding a Javascript Confirm function on a submit button

Posted on 2006-11-26
8
3,082 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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