RequiredFieldValidator not preventing postback


I have a question about what could stop a RequiredFieldValidator from preventing a postback.

I began working on an older, but simple aspx form and my predecessor used manual, server-side form validation (checking to see if some form fields have a value and if not displaying an error message in a label). I thought I'd clean out some unneeded code and replace the manual checking with RequiredFieldValidator controls, but while they appear to be validating, they aren't preventing a postback. Ie., I get my error messages displayed but the postback still occurs.

The form is quite simple and there are no CausesValidation="false" attributes set. My controls look like:

 <asp:TextBox ID="txtPhone" Runat="server" Columns="20" MaxLength="20" />
 <asp:RequiredFieldValidator ID="rfvPhone" runat="server" Display="Dynamic"
      ErrorMessage="* Required" ControlToValidate="txtPhone" />

I created a brand new webform in the same project with just a single textbox, validator and submit button and it acts the same way. Error message displays but postback still occurs.

Is there a global or project-wide setting that would cause this behaviour? Something in the web.config or global.asax?

Best Solution

Whew. Okay, I found the problem, basically by creating a brand new project and comparing its web.config line-by-line with my old project. Turns out the culprit is this:

 <xhtmlConformance mode="Legacy"/>

If I remove the line, my validation works the way I expected it to. Googling that uncovered a bunch of blog posts about how VisualStudio adds that line to the web.config when upgrading web apps from .net 1.1 to .net 3.5.

The blog posts were mainly complaining about how that field interferes with .net's AJAX stuff, but I'm guessing it messes with the JavaScript emitted for the RequiredFieldValidator in a similar fashion.

Related Question