Solved

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

Posted on 2004-03-24
6
279 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

9 Experts available now in Live!

Get 1:1 Help Now