How do you check if validate has been called.

T

tshad

I get the following message from one of my buttons where I have
CauseValidation=false.

Page.IsValid cannot be called before validation has taken place

In my routine I need to do:

if Not IsValid then
exit sub
end if

This is because I have a custom validator on some of my fields and I need to
check this as the function will run regardless of the results of the cusom
validators.

The problem is that I may press the button before any validation has taken
place, which is why I get the error.

Is there a way to check if any validation has taken place, such that I might
do something like:

if some validation done then
if Not IsValid then
exit sub
end if
end if

Thanks,

Tom
 
B

Brock Allen

Are you trying to check Page.IsValid inside of your custom validation events?
This will give back incorrect results since the order of your validators
and the other validators isn't guarenteed.

If you're trying to do this in Page_Load then just call Page.Validate() first.
 
T

tshad

Brock Allen said:
Are you trying to check Page.IsValid inside of your custom validation
events? This will give back incorrect results since the order of your
validators and the other validators isn't guarenteed.

It is a routine that is called by one of my buttons that does not cause
Validation. But I do want to check if the page has been validated and if
NOT - do one thing. If it has gone through validation - do something else.

I don't want to Validate, just check if it has been validated. But I can't
look at Page.IsValid if validation has not been called.

Tom
 
B

Brock Allen

I don't want to Validate, just check if it has been validated. But I
can't look at Page.IsValid if validation has not been called.

Validate always happens after Page_Load but before your server change events
and server click events.
 
T

tshad

Brock Allen said:
Validate always happens after Page_Load but before your server change events
and server click events.

That isn't the question. I am not concerned about at what point in a page
load Validation is done.

My problem is that I press a submit button, that I specifically tell not to
validate, that calls a routine where I am checking IsValid. I am getting an
error because if the page was never validated, this is an error (to check
IsValid).

What I want to do in the routine is check if validation has been done before
I check "IsValid", so I don't get the error.

Thanks,

Tom
 
B

Brock Allen

My problem is that I press a submit button, that I specifically tell
not to validate, that calls a routine where I am checking IsValid. I
am getting an error because if the page was never validated, this is
an error (to check IsValid).

What I want to do in the routine is check if validation has been done
before I check "IsValid", so I don't get the error.

You can always check the sender of the event, cast it to a Button and check
CausesValidation to get the results you want. If it's true then you can check
IsValid if it's false then you can't.
 
T

tshad

Brock Allen said:
You can always check the sender of the event, cast it to a Button and
check CausesValidation to get the results you want. If it's true then you
can check IsValid if it's false then you can't.

But that doesn't tell me if another button causes validation or not.

For example, if I have 2 buttons, button A doesn't call validation and
button B does.

If button A is pushed before button B and I do a "if Not IsValid", then I
will get an error.

Testing button A to see if it causes validation doesn't tell me if another
button has caused it or not.

Tom
 
B

Brock Allen

But that doesn't tell me if another button causes validation or not.

Hmm, ok. Well, I'm sorry that I'm just not getting what you're looking for.
:(
For example, if I have 2 buttons, button A doesn't call validation and
button B does.

Do you have them calling the same server side event handler or different
events handlers?
If button A is pushed before button B and I do a "if Not IsValid",
then I will get an error.

But once you click one button that causes the postback. You don't tend to
get two button clicks on a single postback. If you have two roundtrips (meaning
two button clicks) then it's possible that between postbacks the values of
controls have changed, thus the valid state may have changed. I'm not sure
that helps you, since I don't know what you need.
Testing button A to see if it causes validation doesn't tell me if
another button has caused it or not.

Again, sorry I don't toally grok what what you're trying to do... but perhaps
the validation architecture just doesn't support it. *shrug*
 
M

Marian Kostal

The issue is solved in http://forums.asp.net/thread/1527065.aspx

It explains following code snippet:

using System.Web;
using System.Web.UI;
using System.Reflection;

public class PageUtil
{
public static bool IsPageValidated()
{
Page page = HttpContext.Current.Handler as Page;
if (page == null) throw new HttpException("This method can be called only in classes derived from System.Web.UI.Page");
FieldInfo fieldValidated = typeof(Page).GetField("_validated", BindingFlags.Instance | BindingFlags.NonPublic);
return (bool)fieldValidated.GetValue(page);
}


From http://www.developmentnow.com/g/8_2005_3_0_0_371394/How-do-you-check-if-validate-has-been-called-.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top