Wednesday, January 18, 2012

ASP.NET - Validation control not preventing postback

Issue: ASP.NET RequiredFieldValidator and other validation controls doesn't work the way it used to be all these time. I have a textbox and a RequiredFieldValidator Validation control attached to it. But when i submit the button it postback the page and then displays the validator message. This is not as we would expect. There could be a number of reasons for this. We will analyze different steps to solve it one by one.

1) If you had upgraded the solution from a previous version (1.1 usually) of .NET then in the web.config file, this tag is added and it can cause the issue.
Remove the line and the Validator issue must be fixed.
2) Try explicitly setting "EnableClientScript" property to true for the Validator control. Also make sure you have javascript enabled on the browser.
3) Set "EnableEventValidation" property to true on page level (<%@ Page EnableEventValidation="true" .... %>) or application level in web.config (<pages enableEventValidation="true"/>).
4) If you are using nested virtual directory, then test moving the page to the root directory.
5) Try in Visual Studio command prompt : aspnet_regiis – c . This will copy the folder aspnet_client to all the sub virtual directories.

As general prequation, check if javascript is enabled on client browser. Also always check "Page.IsValid" property in code behind because who knows what happens on client side.