Monday, September 15, 2008

OnClientClick and Form Validation in Asp.net

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.

Hope this helps

1 comment:

Unknown said...

That was useful snippet.
Will start using in my new project.

Also please feel free to add your ASP.NET bookmarks to:

http://www.codebounce.com/ASPNET