RequiredFieldValidator ErrorMessage display

J

James

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
 
G

Guest

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
 
J

James

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?
 
J

James

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
 
J

James

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top