CustomValidator not keeping its isValid=False value

Posted on 2005-03-19
Medium Priority
Last Modified: 2012-06-21

I am using a custom control to validate a file upload control on a webpage. When the file upload fails validation I  set the IsValid property of the custom validator to false.

The aspx code is as follows for the custom validator:

<asp:CustomValidator CssClass="validatorStyle" id="ImageUploadValidator" ControlToValidate="ImageFile"
      runat="server" ErrorMessage="Invalid File Selected For Upload"></asp:CustomValidator>

In the codebehind :

When the page is submitted with a [submit] button I call a function ValidateThePage which does a number of things including checking for the validity of the postedfile.inputstream:

        If (ImageFile.PostedFile.InputStream.Length = 0 And hdnCurrentPage.Value = "3") Then
            ImageUploadValidator.IsValid = False
        End If

When I test for the validation of the page by checking the value of Page.IsValid i get the following error:

Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control with CausesValidation=True or after a call to Page.Validate

When I call the page.validate method before checking for page.isvalid property the isvalid property of the custom validator is set to true from false.

Can anyone think of any reason this is not working for me?

Thanks in advance,

Question by:mausy
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
LVL 23

Accepted Solution

b1xml2 earned 1350 total points
ID: 13582565
Have the user click on a LinkButton which has the attribute CausesValidation="True"
That way when you check on Page.IsValid, it would have already fired off the Page.Validate() method. When in doubt, always fire off the Validate() method before you check the IsValid

Also, check to see if the custom validator is being fired twice....
LVL 12

Assisted Solution

laotzi2000 earned 150 total points
ID: 13582720
That's because the Page_Load function is called before your code to handle the submit
so if you call Page.Validate in Page_Load, the code to validate the upload file is not executed yet.

You can either move the code to validate the upload file to Page_Load,
or call Page.Validate in your Page_Prerender event handler instead of Page_Load.
Page_Prerender will be called after other event handlers.

Author Comment

ID: 13582792
Hi guys,

My problem was I had forgotten the CausesValidation = "True" in my submit button. Therefore as b1xml2 is the closest I feel obliged to offer him the majority of the points. as a token of appreciation to laotzi2000 for the contrib you get "some" points aswell. Hope you don't mind b1xml2!

thanks ppl,
Independent Software Vendors: 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!


Author Comment

ID: 13582855

ahhh jees,  I know I have already given points for the answers but I trust yer good will here......

......As soon as I made that change it worked but now it doesn't!! Now with the causesvalidation=true the submit button isn't even causing postback?!

any idea?
LVL 23

Expert Comment

ID: 13582885
is there any client-side validation failure occurring?

Author Comment

ID: 13583291
ahh yes...well you have definately earned your points... :) that was it alright.
I know I am probably pushing it now...and would gladly put up a separate question with more points but...just to explain my current situation. ... I have 2 different sets of validation controls on 2 layers and I want the controls validated individually. Is it possible to disable the validation controls on one layer while enabling them on another? As I said, if I must I will start a new question with new points....
LVL 23

Expert Comment

ID: 13584740
It would be wise to post another question with say a token amount of points (20 points? ) so that this thread is kept clean.  I'll answer your question there.

Your situation may be faced by other developers and it would be good to keep this PAQ true to its purpose.

Author Comment

ID: 13585072
actually, figured it out...or found the answer rather. On selecting each layer I used the client side javascript function to disable validators on the other layers using the following:

The answer was in fact found on experts exchange so another question would have been redundant anyway.

thanks again b1xml2,


Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 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