Link to home
Start Free TrialLog in
Avatar of RoboRubik
RoboRubik

asked on

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

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{
            SomeMoreFeedback.Text="one";      
      }
}
</script>
<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";
}
</script>
<head>
<title>Validation Test</title>
</head>
<body>
<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"/>
</form>
<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>
</body>
</html>
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

RoboRubik,
>>..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 :
http://oldlook.experts-exchange.com/questions/20910560/Javascript-client-side-validation-for-ASP-NET.html
Avatar of RoboRubik
RoboRubik

ASKER

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.  
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
RobuRubik,
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.