By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 3,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

RequiredFieldValidator ErrorMessage display

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Nov 18 '05 #2

P: n/a
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 <Ry*********@discussions.microsoft.com> wrote in message news:<4E**********************************@microso ft.com>...
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
>

Nov 18 '05 #3

P: n/a
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
Nov 18 '05 #4

P: n/a
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
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.