RequiredFieldValidator ErrorMessage display

Discussion in 'ASP .Net' started by James, Aug 6, 2004.

  1. James

    James Guest

    Hello,

    I have a RequiredFieldValidator for several textbox controls on a
    form. Here's an example with the RequiredFieldValidator.
    EnableClientScript, Enabled, and Visible are set to true for the
    RequiredFieldValidator.

    <asp:textbox id="FirstName" MaxLength="25"
    runat="server"></asp:textbox>

    <asp:requiredfieldvalidator id="RequiredFieldValidator1"
    runat="server" ControlToValidate="FirstName" ErrorMessage="First Name
    is Required"></asp:requiredfieldvalidator>

    If I browse to the form and immediately click the submit button,
    Page.IsValid returns true and I get no ErrorMessage displays. Is this
    by design?

    Also, if I tab through each text box all the way to the submit button,
    the form functions the same way. Is the textbox control not
    considered "having focus" when it's tabbed to?

    The only way I get the ErrorMessage displays is to enter something
    into the controls, tab off, go back to the control, remove what I
    entered, then tab off again. At this point I receive the ErrorMessage
    displays.

    Is this the only way the RequiredFieldValidator works or could I have
    some settings wrong?

    Thanks,
    -Cliff
     
    James, Aug 6, 2004
    #1
    1. Advertising

  2. I don't think the RequiredFieldValidator works this way. If there is no data
    in a textbox and you try to submit the form the RequiredFieldValidator will
    render the page invalid; whether or not the text in the textbox has been
    changed.

    What I meant when I said 'not by design' is that the RFV marks the page
    invlaid when the submit button is clicked, not when the page loads; whence
    the validators obviously do not check validation.

    Maybe the other validation controls work that way, but not the RFV. It would
    be more than useless to have to enter something in a textbox, and then remove
    it in order for it to validate.

    hope this is helpful,
    John


    "Ryan Riddell" wrote:

    > Actually it is by design. .NET assumes the user will get things right the
    > first time. That is why the error messages aren't displayed as soon as the
    > user gets to the page. So the RequiredFieldValidator is true until the value
    > is changed. .NET validators also only check a value if it changed. Why
    > check a value that is in the same state as before? This is why if you enter
    > text, tab off, go back remove text, tab off you get the error message.
    >
    > Onto the server-side of things. Validation happens AFTER page load. So if
    > you do something like.
    >
    > Private Sub Page_Load(...)...
    > Response.Write( Page.IsValid.ToString() )
    > End Sub
    >
    > It will probably just return true (innocent until proven guilty here in the
    > States) because it hasn't had a chance to find invalid data yet.
    >
    > You need to check for Page.IsValid in the event handler methods. By the
    > time the page has reached this point in its life cycle the validation has
    > already been done.
    >
    > HTH,
    > Ryan
    >
    > "James" wrote:
    >
    > > Hello,
    > >
    > > I have a RequiredFieldValidator for several textbox controls on a
    > > form. Here's an example with the RequiredFieldValidator.
    > > EnableClientScript, Enabled, and Visible are set to true for the
    > > RequiredFieldValidator.
    > >
    > > <asp:textbox id="FirstName" MaxLength="25"
    > > runat="server"></asp:textbox>
    > >
    > > <asp:requiredfieldvalidator id="RequiredFieldValidator1"
    > > runat="server" ControlToValidate="FirstName" ErrorMessage="First Name
    > > is Required"></asp:requiredfieldvalidator>
    > >
    > > If I browse to the form and immediately click the submit button,
    > > Page.IsValid returns true and I get no ErrorMessage displays. Is this
    > > by design?
    > >
    > > Also, if I tab through each text box all the way to the submit button,
    > > the form functions the same way. Is the textbox control not
    > > considered "having focus" when it's tabbed to?
    > >
    > > The only way I get the ErrorMessage displays is to enter something
    > > into the controls, tab off, go back to the control, remove what I
    > > entered, then tab off again. At this point I receive the ErrorMessage
    > > displays.
    > >
    > > Is this the only way the RequiredFieldValidator works or could I have
    > > some settings wrong?
    > >
    > > Thanks,
    > > -Cliff
    > >
     
    =?Utf-8?B?Sm9obiBTaXZpbGxh?=, Aug 6, 2004
    #2
    1. Advertising

  3. James

    James Guest

    Thanks guys and I apologize for not mentioning that I'm using a Base
    Page which loads the form using the HtmlForm control. If I do NOT
    derive from my base page, the RFV works as both of you describe.
    Could my base form be causing a problem?

    Ryan Riddell <> wrote in message news:<>...
    > Miscommunication. The RFV will most definitly render the page invalid if the
    > user just hits submit without adding data to the textbox in question.
    > However, the validation is not run until AFTER page_load event, but it is
    > before RaisePostBackEvent. So in page_load nothing has cause the page to be
    > invalid yet, hence it will return valid. After page_load in the event
    > handlers, the validation will have run. So in the case where the user
    > doesn't enter anything, then in Page_Load Page.IsValid is true and in
    > theSubmitButton_Clicked Page.IsValid is false.
    >
    > I hope this version is more clear, let me know if you need further
    > clarification.
    >
    > "John Sivilla" wrote:
    >
    > > I don't think the RequiredFieldValidator works this way. If there is no data
    > > in a textbox and you try to submit the form the RequiredFieldValidator will
    > > render the page invalid; whether or not the text in the textbox has been
    > > changed.
    > >
    > > What I meant when I said 'not by design' is that the RFV marks the page
    > > invlaid when the submit button is clicked, not when the page loads; whence
    > > the validators obviously do not check validation.
    > >
    > > Maybe the other validation controls work that way, but not the RFV. It would
    > > be more than useless to have to enter something in a textbox, and then remove
    > > it in order for it to validate.
    > >
    > > hope this is helpful,
    > > John
    > >
    > >
    > > "Ryan Riddell" wrote:
    > >
    > > > Actually it is by design. .NET assumes the user will get things right the
    > > > first time. That is why the error messages aren't displayed as soon as the
    > > > user gets to the page. So the RequiredFieldValidator is true until the value
    > > > is changed. .NET validators also only check a value if it changed. Why
    > > > check a value that is in the same state as before? This is why if you enter
    > > > text, tab off, go back remove text, tab off you get the error message.
    > > >
    > > > Onto the server-side of things. Validation happens AFTER page load. So if
    > > > you do something like.
    > > >
    > > > Private Sub Page_Load(...)...
    > > > Response.Write( Page.IsValid.ToString() )
    > > > End Sub
    > > >
    > > > It will probably just return true (innocent until proven guilty here in the
    > > > States) because it hasn't had a chance to find invalid data yet.
    > > >
    > > > You need to check for Page.IsValid in the event handler methods. By the
    > > > time the page has reached this point in its life cycle the validation has
    > > > already been done.
    > > >
    > > > HTH,
    > > > Ryan
    > > >
    > > > "James" wrote:
    > > >
    > > > > Hello,
    > > > >
    > > > > I have a RequiredFieldValidator for several textbox controls on a
    > > > > form. Here's an example with the RequiredFieldValidator.
    > > > > EnableClientScript, Enabled, and Visible are set to true for the
    > > > > RequiredFieldValidator.
    > > > >
    > > > > <asp:textbox id="FirstName" MaxLength="25"
    > > > > runat="server"></asp:textbox>
    > > > >
    > > > > <asp:requiredfieldvalidator id="RequiredFieldValidator1"
    > > > > runat="server" ControlToValidate="FirstName" ErrorMessage="First Name
    > > > > is Required"></asp:requiredfieldvalidator>
    > > > >
    > > > > If I browse to the form and immediately click the submit button,
    > > > > Page.IsValid returns true and I get no ErrorMessage displays. Is this
    > > > > by design?
    > > > >
    > > > > Also, if I tab through each text box all the way to the submit button,
    > > > > the form functions the same way. Is the textbox control not
    > > > > considered "having focus" when it's tabbed to?
    > > > >
    > > > > The only way I get the ErrorMessage displays is to enter something
    > > > > into the controls, tab off, go back to the control, remove what I
    > > > > entered, then tab off again. At this point I receive the ErrorMessage
    > > > > displays.
    > > > >
    > > > > Is this the only way the RequiredFieldValidator works or could I have
    > > > > some settings wrong?
    > > > >
    > > > > Thanks,
    > > > > -Cliff
    > > > >
     
    James, Aug 9, 2004
    #3
  4. James

    James Guest

    Found the solution here.
    http://www.dotnetjunkies.net/Forums/ShowPost.aspx?PostID=1234

    BasePage.cs

    protected void CopyValidators(ValidatorCollection src,
    ValidatorCollection dest)
    {
    IEnumerator myEnum = src.GetEnumerator();
    while(myEnum.MoveNext())
    {
    dest.Add((IValidator)myEnum.Current);
    }
    }


    protected override void OnInit(EventArgs e)
    {
    ValidatorCollection mySavedValidators = new ValidatorCollection();
    CopyValidators(Page.Validators, mySavedValidators); // Save page
    validators
    BuildFrame();
    MoveControls();
    CopyValidators(mySavedValidators, Page.Validators); // restore
    validators back to the page
    base.OnInit(e);
    } //protected override void OnInit
     
    James, Aug 17, 2004
    #4
  5. James

    James Guest

    Found the solution here.
    http://www.dotnetjunkies.net/Forums/ShowPost.aspx?PostID=1234

    BasePage.cs

    protected void CopyValidators(ValidatorCollection src,
    ValidatorCollection dest)
    {
    IEnumerator myEnum = src.GetEnumerator();
    while(myEnum.MoveNext())
    {
    dest.Add((IValidator)myEnum.Current);
    }
    }


    protected override void OnInit(EventArgs e)
    {
    ValidatorCollection mySavedValidators = new ValidatorCollection();
    CopyValidators(Page.Validators, mySavedValidators); // Save page
    validators
    BuildFrame();
    MoveControls();
    CopyValidators(mySavedValidators, Page.Validators); // restore
    validators back to the page
    base.OnInit(e);
    } //protected override void OnInit
     
    James, Aug 17, 2004
    #5
    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. Christian Ista
    Replies:
    5
    Views:
    3,195
    Johann MacDonagh
    Dec 7, 2004
  2. Nathan Sokalski
    Replies:
    1
    Views:
    9,116
    Peter Blum
    Jun 29, 2005
  3. Replies:
    1
    Views:
    377
  4. =?iso-8859-1?Q?Andr=E9_Nobre?=

    Custom Control and RequiredFieldValidator.Display

    =?iso-8859-1?Q?Andr=E9_Nobre?=, Jul 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,045
    =?iso-8859-1?Q?Andr=E9_Nobre?=
    Jul 27, 2006
  5. AG
    Replies:
    5
    Views:
    4,356
    turkush
    Sep 1, 2007
Loading...

Share This Page