Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why doesn't custom validator work?

Posted on 2010-08-12
9
Medium Priority
?
261 Views
Last Modified: 2013-11-08
In asp.net 3.5, I'm using a custom validator.  I set a breakpoint in the code behind.  It is never hit and I never see an error display.  How can I figure what is wrong with the implementation?
<asp:CustomValidator ID="myVal" ControlToValidate="txtTextBox" 
                    runat="server" Display="Dynamic" EnableClientScript="false" 
                    ErrorMessage="An error has occurred" 
                    OnServerValidate="IsThisValid/>

//In code behind
protected void IsThisValid(object sender, ServerValidateEventArgs args)
        {
            args.IsValid = MyValidator.IsThisValid(args.Value);
        }

Open in new window

0
Comment
Question by:brettr
  • 4
  • 4
9 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 33424640
just wondering if that's a typo or missing " after "IsThisValid?

OnServerValidate="IsThisValid"/>
0
 

Author Comment

by:brettr
ID: 33424667
Typo.  The quote is there in the real code.
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33424679
<asp:TextBox runat="server" id="txtCustom" />
<asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtCustom" onservervalidate="IsThisValid"
errormessage="The text must have a value!" />


//Code Behind.
protected void IsThisValid(object sender, ServerValidateEventArgs e)
{
    if(e.Value.Length == "")
        e.IsValid = true;
    else
        e.IsValid = false;
}

Can you try this code and see what it returns.

Good Luck.
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 41

Expert Comment

by:guru_sami
ID: 33424700
Then you might want to make sure if no other client-side validation is failing.....and the isn't any postback at all.
0
 

Author Comment

by:brettr
ID: 33424750
@lucky85:

I tried your example.  Still nothing.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 total points
ID: 33424827
Did you set ValidationGroup for your submit button?
If so then you need to include that for your CustomValidator as well.
0
 

Author Comment

by:brettr
ID: 33424834
Apparently I needed to add a ValidationGroup attribute for this to work.  I'm using a value that matches that used by other validators.  Not sure why ValidationGroup causes it to work now.
0
 

Author Closing Comment

by:brettr
ID: 33424892
I posted just after you sami.  Thanks.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33424900
If you Button has ValidationGroup="abc" then when you click that button the Validators that has ValidationGroup="abc" are only validated.
So if you don't include it with your CustomValidator, it won't fire.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

773 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