Custom Validator Only Fires OnServerValidate When Data Exists

R

Rigs

Hi,

I have a textbox with a Custom Validator that utilizes the OnServerValidate
method for that textbox. This works fine, however the method only executes
when data exists in that textbox after the Submit button is clicked. If I
click the submit button and no data exists in the textbox, the
OnServerValidate method does not fire.

I'd like the OnServerValidate method to either execute every time the Submit
button is clicked. I am also open to suggestions on an alternative method
to validate the control. I can not utilize a RequiredFieldValidator because
data in other textboxes in the web form determine if the textbox in question
should contain data or not. It's a little complicated, hence the use of the
Custom Validator.

If anyone has any suggestions, I'd love to hear them.

Thanks,
-Rigs
 
W

William F. Robertson, Jr.

The CustomValidator along with all other built in validators only fire when
data is present. ie .Text.Length != 0, otherwise the validation returns
true.

The two options I can think of, I am sure there are more are:

1. Derive from BaseValidator and override the EvaluateIsValid method. This
will fire everytime regardless of what is in the control.
2. Derive from CustomValidator and override the EvaluateIsValid method.

The second approach is probably what you will want to go for.

//this is kinda code, it should work, but I really didn't test it.
protected override bool EvaluateIsValid()
{
string s = GetControlValidationValue( this.ControlToValidate );
return this.OnServerValidate( s );
}

HTH,

bill
 
R

Rigs

Bill,

Thanks for the advice. Unfortunately, I'm still a little green with ASP
..NET. Could you expand more on what you mean in your response? How would I
implement your suggestion? Any additional comments would be appreciated.

Thanks,
-Rigs
 
W

William F. Robertson, Jr.

I am assuming you are writing this in C#.

You will add this line to the top of your aspx page (not codebehind)

<%@ Register TagPrefix="myControl" Namespace="tempasp" Assembly="tempasp" %>

Here is the control to validate. The dascweb: prefix is my own stuff, it
would normally be asp:TextBox, but that really doesn't matter
<dascweb:TextBoxEx ID="txtPhone" Runat="server"
IsRequired="False"></dascweb:TextBoxEx>

this is the validation control.
<mycontrol:MyCustomValidator id="valCustom" runat="server"
ControlToValidate="txtPhone" ErrorMessage="Message is in
error."></mycontrol:MyCustomValidator>

Now somewhere in your solution you will need to define the control.

My solution is tempasp, so the namespace for all my files in tempasp.

Here is my control code:
public class MyCustomValidator : CustomValidator
{
protected override bool EvaluateIsValid()
{
string s = this.GetControlValidationValue( this.ControlToValidate );
return this.OnServerValidate( s );
}

}

This will bypass the null / String.Empty check for the built in
CustomValidator control and trigger your ServerValidate event that you are
hoping to receive.

If this still doesn't help you post an email address I can sent some files
to you.

HTH,

bill
 
R

Rigs

Bill,

Thank you very much for the detailed response. That is what I was looking
for.

Unfortunately, I am writing this VB. NET and not familiar with C#. So, I'm
not following your C# code completely. What event triggers the
EvaluateIsValid() function? Could you re-pseudo code for VB? My email is:
(e-mail address removed) if you'd like to send directly to me.

Thanks,
-Rigs
 
R

Rigs

Bill,

FYI... Our mail server is went down about an hour ago. So, if you try to
email, it may get rejected. We are rebuilding the mailboxes now and should
be up an running in about another hour. Let's use nntp for the time being
if you are still inclined to assist me with this issue.

Thanks,
-Rigs
 
W

William F. Robertson, Jr.

Here is the code for the class in vb.net.

Public Class MyCustomValidator Inherits CustomValidator

Protected Overrides Function EvaluateIsValid() As Boolean
Dim text As String = MyBase.GetControlValidationValue(
MyBase.ControlToValidate )
Return Me.OnServerValidate(text1)
End Function

End Class

Now all the aspx tag stuff will work the same regardless of the language.
(one great quality of .net)

The EvaluateIsValid function isn't really triggered by an event. When
Page.Validate() is called, the page will step through all the controls on
the page (really it has a list, but same basic idea). Any controls in the
control tree that implement the IValidator interface have a EvaluateIsValid
method. The Page will then call EvaluateIsValid on every control.

The EvaluateIsValid method for the CustomValidator triggers the
ServerValidate event. Here is a psuedo code implementation of what I think
the CustomValidator's EvaluateIsValid method looks like:

Protected Overrides Function EvaluateIsValid() As Boolean
Dim text As String =
MyBase.GetControlValidationValue(MyBase.ControlToValidate)

if ( ( text Is Nothing ) Or ( text.Trim.Length = 0 ) ) Then
return true
end if

' OnServerValidate is the method that triggers the event.
' this is why it wasn't getting called when data was not present.
Return Me.OnServerValidate(text)

End Function
 
R

Rigs

Bill,

Thanks again for your assistance. Unfortunately, I am still struggling with
this issue.

I added a new Class file (a .vb file) to this project. I copied and pasted
the code you provided into that class file. I re-built the solution
afterwards. The build had no errors. I launched the web page in IE 6.0.
It had to effect. The validation did not occur until a character was
entered into the textbox. Does it matter that the OnServerValidate code is
located on the web page's code behind page and this new class was added to a
separate file? How does the web page's code behind page even know that the
new class exists for it to have the option to execute the MyCustomValidator
class?

I'm not confident I implemented your suggestion correctly. Is that how you
would have gone about it? Please advise.
Thanks,
-Rigs
 
W

William F. Robertson, Jr.

I sent an email to your address from earlier with a zipped VB project
demostrating the feature I think you are looking for.

Let me know how it works out.

Thanks,

bill
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top