Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > RequiredFieldValidator ErrorMessage display

Reply
Thread Tools

RequiredFieldValidator ErrorMessage display

 
 
James
Guest
Posts: n/a
 
      08-06-2004
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9obiBTaXZpbGxh?=
Guest
Posts: n/a
 
      08-06-2004
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
> >

 
Reply With Quote
 
 
 
 
James
Guest
Posts: n/a
 
      08-09-2004
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
> > > >

 
Reply With Quote
 
James
Guest
Posts: n/a
 
      08-17-2004
Found the solution here.
http://www.dotnetjunkies.net/Forums/...px?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
 
Reply With Quote
 
James
Guest
Posts: n/a
 
      08-17-2004
Found the solution here.
http://www.dotnetjunkies.net/Forums/...px?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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView RequiredFieldValidator Display AG ASP .Net 5 09-01-2007 11:15 AM
Custom Control and RequiredFieldValidator.Display =?iso-8859-1?Q?Andr=E9_Nobre?= ASP .Net 0 07-27-2006 09:28 PM
ErrorMessage display with validators paul.hester@gmail.com ASP .Net 1 05-29-2006 08:45 PM
RequiredFieldValidator's Display="Dynamic" acts different when selecting from previously used entries Nathan Sokalski ASP .Net 1 06-29-2005 06:54 PM
Display message when RequiredFieldValidator fired Christian Ista ASP .Net 5 12-07-2004 08:29 AM



Advertisments