how do I make the validator cut off other code on the page from executing?

Discussion in 'ASP .Net' started by bennett@peacefire.org, Jun 1, 2005.

  1. Guest

    At
    http://www.brainjammer.com/testing/validator_test.aspx
    I have a text field where you can enter text, and a button where if you
    click the button, it sets the value of a label below it, to display the
    text you entered in a text field. There is also a
    RequiredFieldValidator for the text field so that you get an error if
    you leave it blank.

    As long as I have EnableClientScript set to true for the
    RequiredFieldValidator, if I leave the text field blank and click the
    button (ignore what the button caption says, "hide this panel" etc.),
    the RequiredFieldValidator shows, and the text of the label below it
    does not get set to blank. i.e., when the validator detects an error
    in the field, it stops the other button click handling code from
    running, which is what you'd expect.

    The problem is that if EnableClientScript for the validator is set to
    false (which is how I have it set now), then when you enter blank for
    the text field and click the button, the validator still displays,
    *but* the text of the label below it also changes to blank. So when
    the validator is set to only do server-side validation, it doesn't cut
    off the other button click handling code from executing when it detects
    an error.

    Is there any way to make the validator stop the button's onclick event
    code from running if the validator detects an error in the field that's
    submitted?

    I can just set EnableClientScript to true, but some of our users have
    JavaScript turned off in their browser, which appears to have the same
    effect as having EnableClientScript set to false on the ASP page -- the
    button's onclick event handler runs to completion even if the validator
    detects an error in the field.

    Of course instead of using a validator, I could just put some code in
    the button's onclick event handler that validates the control myself,
    and displays the error and exits if something's wrong, but it's more
    elegant to use the built-in validators if possible.
     
    , Jun 1, 2005
    #1
    1. Advertising

  2. Peter Blum Guest

    I think you are asking how to test if the page is invalid before
    saving/redirecting, etc.

    *Always* test Page.IsValid is true in your Click event method before saving.
    Its not done automatically:

    Protected Sub Submit_Click(parameters)
    If Page.IsValid Then
    ' your code to save/redirect, etc
    End If
    End Sub

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx

    <> wrote in message
    news:...
    > At
    > http://www.brainjammer.com/testing/validator_test.aspx
    > I have a text field where you can enter text, and a button where if you
    > click the button, it sets the value of a label below it, to display the
    > text you entered in a text field. There is also a
    > RequiredFieldValidator for the text field so that you get an error if
    > you leave it blank.
    >
    > As long as I have EnableClientScript set to true for the
    > RequiredFieldValidator, if I leave the text field blank and click the
    > button (ignore what the button caption says, "hide this panel" etc.),
    > the RequiredFieldValidator shows, and the text of the label below it
    > does not get set to blank. i.e., when the validator detects an error
    > in the field, it stops the other button click handling code from
    > running, which is what you'd expect.
    >
    > The problem is that if EnableClientScript for the validator is set to
    > false (which is how I have it set now), then when you enter blank for
    > the text field and click the button, the validator still displays,
    > *but* the text of the label below it also changes to blank. So when
    > the validator is set to only do server-side validation, it doesn't cut
    > off the other button click handling code from executing when it detects
    > an error.
    >
    > Is there any way to make the validator stop the button's onclick event
    > code from running if the validator detects an error in the field that's
    > submitted?
    >
    > I can just set EnableClientScript to true, but some of our users have
    > JavaScript turned off in their browser, which appears to have the same
    > effect as having EnableClientScript set to false on the ASP page -- the
    > button's onclick event handler runs to completion even if the validator
    > detects an error in the field.
    >
    > Of course instead of using a validator, I could just put some code in
    > the button's onclick event handler that validates the control myself,
    > and displays the error and exits if something's wrong, but it's more
    > elegant to use the built-in validators if possible.
    >
     
    Peter Blum, Jun 2, 2005
    #2
    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. adiel
    Replies:
    1
    Views:
    1,019
    makthar
    Nov 24, 2003
  2. novel
    Replies:
    3
    Views:
    398
    David Postill
    Dec 7, 2003
  3. Replies:
    2
    Views:
    2,206
    Mark Parnell
    Aug 24, 2004
  4. Bodidily
    Replies:
    0
    Views:
    463
    Bodidily
    Aug 25, 2004
  5. Marc
    Replies:
    5
    Views:
    6,108
Loading...

Share This Page