CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, September 15, 2008

OnClientClick and Form Validation in Asp.net

image

When we use OnClienClick script for a button in asp.net, you can notice that all the associated validation controls (validation group) will fail to validate. This happens because the OnClientClick script overrides form validation script.

To fix this, here is the work around.Add the below code in Page Load event

Button1.Attributes.Add("onclick", "if(confirm('do this ?') == false) return false;");

This will execute the client script and then executes the form validations scripts before post back.

Post a Comment

Anonymous said...

Found a better solution at
http://www.stevekinsey.com/2007/06/04/onclientclick-and-form-validation-controls/

This does validation before prompting user 'Are you sure?'.

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