Solved

Adding a Javascript Confirm function on a submit button

Posted on 2006-11-26
8
3,083 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 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
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
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create an MVC5, Razor Widget 2 51
VS 2015 is giving [No relevant source lines] 2 49
asp.net mvc 2 44
Using the .NET JSON Serializer to create a single string 6 59
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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

751 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