?
Solved

ASP.NET Validators not preventing postback when validation fails

Posted on 2009-05-20
10
Medium Priority
?
3,124 Views
Last Modified: 2013-12-17
I have a form and some asp:textboxes and some validators. If the information isn't valid, I don't want it to postback. Seems pretty simple, but can't seem to get it to work like this. I've tried removing the <xhtmlConformance mode> tag, I've played around with various combinations using OnClientClick, EnableClientScript, and CausesValidation, and trying to return false to the ImageButton if it fails, etc. Nothing seems to prevent the postback when the input is invalid. Help!

This would be easy in classic asp, but I'm trying to learn this new .NET stuffs...

Two examples of code I've tried are below. We're using .NET 2.0 C#.

Oh, also I'll toss an assist to anyone able to tell my why my code isn't doing the popup for the ValidationSummary even though I have ShowMessageBox="true" and ShowSummary="false".
function ValidateForm() {
    if (Page_IsValid) {
        document.getElementById("btn_Submit").disabled = true;
        return true;
    } else {
        return false;
    }
}
 
...
 
<form runat="server" id="main_form" onsubmit="Form_Submit">
     Email address
     <asp:TextBox ID="email1" name="email1" runat="server" size="15" maxlength="55" CausesValidation="true" />&nbsp;&nbsp;&nbsp;&nbsp;
      Reenter your email address 
     <asp:TextBox ID="email2" name="email2" runat="server" size="15" maxlength="55" CausesValidation="true" /><br />
     <asp:ImageButton ImageUrl="btn_go.gif" ID="btn_Submit" name="btn_Submit" CausesValidation="true" OnClientClick="javascript:return ValidateForm();" runat="server" Text="Submit" />
     <asp:CompareValidator id="CompareEmails" runat="server" ErrorMessage="Email addresses do not match." Display="dynamic" ControlToValidate="email1" ControlToCompare="email2" Type="String" EnableClientScript="true" />
     <asp:RequiredFieldValidator ID="RequiredEmail1" runat="server" ErrorMessage="Please enter the same email address in both boxes." ControlToValidate="email1" />
     <br /><asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please fix the following:" EnableClientScript="true" ShowMessageBox="true" ShowSummary="false" ForeColor="red" BackColor="yellow" />
</form>
 
___________another way I've tried it below________________________
 
 
<form runat="server" id="main_form">
      Email address
     <asp:TextBox ID="email1" name="email1" runat="server" size="15" maxlength="55"  />&nbsp;&nbsp;&nbsp;&nbsp;
      Reenter your email address 
     <asp:TextBox ID="email2" name="email2" runat="server" size="15" maxlength="55"  /><br />
      <asp:ImageButton ImageUrl="btn_go.gif" ID="btn_Submit" name="btn_Submit"  OnClick="Form_Submit" runat="server" Text="Submit" />
     <asp:CompareValidator id="CompareEmails" runat="server" ErrorMessage="Email addresses do not match." Display="dynamic" ControlToValidate="email1" ControlToCompare="email2" Type="String" />
     <asp:RequiredFieldValidator ID="RequiredEmail1" runat="server" ErrorMessage="Please enter the same email address in both boxes." ControlToValidate="email1" />
      <br /><asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please fix the following:"  ShowMessageBox="true" ShowSummary="false" ForeColor="red" BackColor="yellow" />
</form>

Open in new window

0
Comment
Question by:paulott
[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
  • 4
  • 3
  • 3
10 Comments
 
LVL 10

Expert Comment

by:orbulat
ID: 24437479
perhaps try this?

document.getElementById('btn_Submit').disabled = 'disabled'
or
document.getElementById('btn_Submit').style.visibility = 'hidden'

maybe u can use an alternative
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_21210052.html
0
 
LVL 1

Author Comment

by:paulott
ID: 24437498
Hey orbulat, thanks for the comment.

I'm not worried about the button being pushed twice. What I don't want is a postback to the server when the first level of validation is supposed to be handled in the client.  For example, if they did not enter an email1 address, I don't want it to post to the server, then come back with the error message that is in the RequiredFieldValidator. I want it to just display the error in the RequiredFieldValidator  via the client and javascript. Does that make sense?
0
 
LVL 10

Expert Comment

by:orbulat
ID: 24438434
i get what u mean, i think i came across sth. like this before
eventually i make the imagebutton always return false and run a js function
the js function contains validator's work, only do the post when it's valid.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24440324
If you have the Required Field validator on the email1 field, you should also have a Required Field validator on the email2 field to guarantee something has been entered into both fields. The Compare validator does not validate that the fields you reference have a value, and can cause problems when you are expecting a value where one does not exist.
0
 
LVL 1

Author Comment

by:paulott
ID: 24442362
orbulat, yeah, I know I could do this in classic ASP with javascript, which is what makes it frustrating when these ASP.NET controls don't seem able to do it.

Shaun, if I use a Required for email1 and Compare email1 to email2, you're saying it will allow if email1 has "123" and email2 is blank? You mean the Compare doesn't compare empty values to filled values?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24442835
I just verified that the Compare validator will handle checking "123" against blank. (Old habits, I guess.) I was able to take your code, modify it (removing the javascript function) and verify that this will work in a straight ASPX page, at least in IE 6 and FireFox.
     Email address
     <asp:TextBox ID="email1" name="email1" runat="server" size="15" maxlength="55" />    
      Reenter your email address 
     <asp:TextBox ID="email2" name="email2" runat="server" size="15" maxlength="55" /><br />
     <asp:ImageButton ImageUrl="~/accept.gif" ID="btn_Submit" name="btn_Submit" CausesValidation="true" runat="server" Text="Submit" />
     <asp:CompareValidator id="CompareEmails" runat="server" ErrorMessage="Email addresses do not match." Display="dynamic" ControlToValidate="email1" ControlToCompare="email2" Type="String" EnableClientScript="true" />
     <asp:RequiredFieldValidator ID="RequiredEmail1" runat="server" ErrorMessage="Please enter the same email address in both boxes." ControlToValidate="email1" EnableClientScript="true" />
     <br /><asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please fix the following:" EnableClientScript="true" ShowMessageBox="true" ShowSummary="false" ForeColor="red" BackColor="yellow" />
     <asp:Label ID="Results" runat="server" />

Open in new window

0
 
LVL 1

Author Comment

by:paulott
ID: 24442859
It handles the validation client side before the postback?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24442938
It does handle the validation client-side. If the email1 field has not been modified and the field loses focus, the Required Field validator does not fire, but when the ImageButton is clicked, the validator does fire. (I'm guessing that this behavior was by design to allow the user to tab through fields and not get the pesky error messages unless they actually did something to the field.)
0
 
LVL 10

Expert Comment

by:orbulat
ID: 24447159
here is my example on requirevalidator and comparevalidator
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test Validator</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Txt1: <asp:TextBox runat="server" ID="txtBox1"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="reqVal1" ControlToValidate="txtBox1" ErrorMessage="please enter txtbox1"></asp:RequiredFieldValidator>
        <br />
        Txt2: <asp:TextBox runat="server" ID="txtBox2"></asp:TextBox>
        <asp:CompareValidator runat="server" ID="compVal1" ControlToValidate="txtBox1" ControlToCompare="txtBox2" ErrorMessage="txt1 =/= txt2!" ></asp:CompareValidator>
        <br />                
        <asp:ImageButton runat="server" ID="imgbut1" ImageUrl="button1.gif" CausesValidation="true" AlternateText="click" OnClick="imgbut1_Click" />
        
    </div>
    </form>
</body>
</html>
 
codebehind -->
 
  protected void imgbut1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("validation is ok");
    }

Open in new window

0
 
LVL 1

Accepted Solution

by:
paulott earned 0 total points
ID: 24456654
I had to change the OnClientClick in my first example to "if (!ValidateForm()) return false;" and added a ValidationGroup to them. That seemed to help prevent it from posting back to the server for validation and handling it completely in the client. Thanks for trying guys.
0

Featured Post

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

741 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