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
  • 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,
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!


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

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.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

580 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