Solved

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

Posted on 2004-03-24
6
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html Table Looping (part 2) 5 39
Record locking on classic ASP 3 58
Read text on Table 7 44
PHP $_POST vs asp request 4 53
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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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