ASP.Net

Yes, believe it or not I am going to write an on-topic post on my blog. Not only that but a programming post even, shocking I know. But I had a particular problem today and I thought the solution was worth sharing.

So I had a form, fairly simple form, a handful of textboxes and dropdown boxes and a submit button. I had some validation controls on there to ensure that the data was entered correctly and the form was working fine. I decided to add a confirmation message to the onclientlclick event of the submit button. You know the sort of thing, a popup button that says “Are you sure you want to perform this action” with an ok and cancel button. So I added the following code to the onclientlclick property of the button.

return confirm('Are you sure you want to perform this action?')

This had an unexpected result, when you click ok on the popup window, it ignored all of the form validation controls and submitted the form whatever was filled in. Well after much digging around google, I found the following solution. You need to wrap the javascript in an if statement and check the Page_ClientValidate property. So all you need to do is replace the code in the onclientlclick property with the following.

if (Page_ClientValidate()){return confirm('Are you sure you want to perform this action?')}

This now works perfectly, the popup doesn’t even appear unless all of the validation requirements have been met. A useful little piece of javascript I though, hope it will be of some use to you.

Similar Posts

« »