Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding a Javascript Confirm function on a submit button

Posted on 2006-11-26
8
Medium Priority
?
3,092 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
[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
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 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