Solved

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

Posted on 2004-03-24
6
280 Views
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{
            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>
0
Comment
Question by:RoboRubik
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 10674500
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:8080/Programming/Programming_Languages/Dot_Net/Q_20910560.html
0
 

Author Comment

by:RoboRubik
ID: 10675176
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.  
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 10676567
RoboRubik ,
Have you try to re-install .NET Framework?It this problems happend to your recently or it doesn't work at all from the beginning?It work in my pc as a charm, no problems.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 10676608
RobuRubik,
Alternatively, you can fix it using
aspnet_regiis -i
Run the above command from command line.
0
 

Author Comment

by:RoboRubik
ID: 10677394
Could you please explain what you mean that it worked on your PC?
0
 

Author Comment

by:RoboRubik
ID: 10677483
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now