Solved

Why doesn't custom validator work?

Posted on 2010-08-12
9
251 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
Comment Utility
just wondering if that's a typo or missing " after "IsThisValid?

OnServerValidate="IsThisValid"/>
0
 

Author Comment

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

Expert Comment

by:Anil Golamari
Comment Utility
<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
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
Then you might want to make sure if no other client-side validation is failing.....and the isn't any postback at all.
0
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

 

Author Comment

by:brettr
Comment Utility
@lucky85:

I tried your example.  Still nothing.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I posted just after you sami.  Thanks.
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
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

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

10 Experts available now in Live!

Get 1:1 Help Now