[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Why doesn't custom validator work?

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
brettr
Asked:
brettr
  • 4
  • 4
1 Solution
 
guru_samiCommented:
just wondering if that's a typo or missing " after "IsThisValid?

OnServerValidate="IsThisValid"/>
0
 
brettrAuthor Commented:
Typo.  The quote is there in the real code.
0
 
Anil GolamariCommented:
<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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
guru_samiCommented:
Then you might want to make sure if no other client-side validation is failing.....and the isn't any postback at all.
0
 
brettrAuthor Commented:
@lucky85:

I tried your example.  Still nothing.
0
 
guru_samiCommented:
Did you set ValidationGroup for your submit button?
If so then you need to include that for your CustomValidator as well.
0
 
brettrAuthor Commented:
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
 
brettrAuthor Commented:
I posted just after you sami.  Thanks.
0
 
guru_samiCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now