Solved

Why doesn't custom validator work?

Posted on 2010-08-12
9
256 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
[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
  • 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
Technology Partners: 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!

 
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 500 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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