We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Why can't I get ASP.NET validation controls to work?

RoboRubik asked
Medium Priority
Last Modified: 2007-12-19
I am trying to use ASP.NET wit C# for form validation.  The problem is that I cannot seem to get any validation controls to prevent anything.  I can get them to show the validation text, but it does not seem to prevent the form from "submitting".  I have seen exmples in books and online showing how the validation controls should prevent something from happening... usually an OnClick event associated with the button.  But, when I try them the event always happens, even if though the validation error may show up.  I have never got an example to work right, even when I copy and paste the code, could there be an IIS or web.config problem?  Maybe I am misunderstanding what these controls should do.  I have an example below of a test form... when I click either button, both feedback labels change.  How can I check for validation?

<%@ Page Language="C#" Debug="true" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
  if (IsPostBack && SomeMoreFeedback.Text=="one") {
            SomeMoreFeedback.Text="the other";      
      } else{
<script runat="server">
void btnSubmit_OnClick(Object Src, EventArgs E)
      SomeFeedback.Text="Button 1 Clicked";
void btnSubmit2_OnClick(Object Src, EventArgs E)
      SomeFeedback.Text="Button 2 Clicked";
<title>Validation Test</title>
<form name="testform" id="testform" runat="server">
<asp:textbox ID="TestBox" runat="server" TextMode="SingleLine"/>
<asp:requiredfieldvalidator ControlToValidate="TestBox" ErrorMessage="Required!!!" ID="valName" runat="server" />
<asp:button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_OnClick"/>
<asp:button ID="btnSubmit2" runat="server" Text="Submit" OnClick="btnSubmit2_OnClick"/>
<asp:label ID="SomeFeedback" runat="server" Text="" Visible="true"></asp:label><br />
<asp:label ID="SomeMoreFeedback" runat="server" Text="Form Not Submitted" Visible="true"></asp:label>
Watch Question

David H.H.LeeDigital Marketing (Hospitality)

>>..The problem is that I cannot seem to get any validation controls to prevent anything. ...
-If you use Requirefieldvalidator, you just need to assign a spesific value into ControlToValidate property. However, this only will help you to make sure the required field is not blank before submit the form. To make a more complicate validator, you can use CustomValidator control to solve the problems. More details, please take a look this PAQ :


I have assigned a ControlToValidate property (see code in question).  The validate is attached to the control because it will display the ErrorMessage property text if the field "TestBox" is blank, but the form submits anyway.  I am looking for an example in C# that demonstrates how the validate prevents something if the criteria is not met but permits it if it is.  I have done plenty of validation with ASP, PHP, and JavaScript, so I am not worried about complex validation I would just like to understand why I cannot even get a simple required field validation to work.

I doubt it matters, but just in case, I have .Net Framework 1.1 installed on Windows 2000 Server.  I also have .Net 1.1 installed on XP Pro wit IIS 5.1 which acts the same. I think I am just misunderstanding something fundamental.  In the end, I just want to make sure that records are not inserted into a DB unless they are validated... as is, the form will tell you that you need this and that, but the record gets inserted anyway.  Same thing with other aspx forms I am working on.  I could rewrite them in ASP, but obviously I would rather understand what I am doing or have set up wrong.  
Digital Marketing (Hospitality)
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
David H.H.LeeDigital Marketing (Hospitality)

Alternatively, you can fix it using
aspnet_regiis -i
Run the above command from command line.


Could you please explain what you mean that it worked on your PC?


Oh, and to answer your other question, this is not something that happened recently, I have never been able to get ASP.NET validation controls to do anything other than display warning text.  It acts the same on both servers I have set up, so if it is a problem with the setup I think it would be because of the version of the .NET framework I am using.  Most of the examples I have tried that do not behave as advertised were created with the .Net framework beta release, so I wonder if the validation control behavior has changed in  some way since then.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.