How do you check if validate has been called.

Discussion in 'ASP .Net' started by tshad, Mar 31, 2005.

  1. tshad

    tshad Guest

    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
    tshad, Mar 31, 2005
    #1
    1. Advertising

  2. tshad

    Brock Allen Guest

    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.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > 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
    >
    Brock Allen, Mar 31, 2005
    #2
    1. Advertising

  3. tshad

    tshad Guest

    "Brock Allen" <> wrote in message
    news:...
    > 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
    >
    > If you're trying to do this in Page_Load then just call Page.Validate()
    > first.
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    >> 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
    >>

    >
    >
    >
    tshad, Apr 6, 2005
    #3
  4. tshad

    Brock Allen Guest

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

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen
    Brock Allen, Apr 6, 2005
    #4
  5. tshad

    tshad Guest

    "Brock Allen" <> wrote in message
    news:...
    > > 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.


    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
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    tshad, Apr 10, 2005
    #5
  6. tshad

    Brock Allen Guest

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

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen
    Brock Allen, Apr 11, 2005
    #6
  7. tshad

    tshad Guest

    "Brock Allen" <> wrote in message
    news:...
    >> 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.


    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
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    tshad, Apr 11, 2005
    #7
  8. tshad

    Brock Allen Guest

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

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen
    Brock Allen, Apr 11, 2005
    #8
  9. 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
    Marian Kostal, Jan 11, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul J. Lucas
    Replies:
    28
    Views:
    6,119
    Dale King
    Dec 5, 2003
  2. Replies:
    1
    Views:
    17,891
  3. Michele Simionato
    Replies:
    11
    Views:
    437
    vasudevram
    Dec 23, 2006
  4. Ankur
    Replies:
    4
    Views:
    2,588
    Ankur
    Jan 12, 2009
  5. Dave DiFilippo
    Replies:
    0
    Views:
    813
    Dave DiFilippo
    Feb 24, 2009
Loading...

Share This Page