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

Custom Validator Only Fires OnServerValidate When Data Exists

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


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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:eq**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #2

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

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:eq**************@TK2MSFTNGP11.phx.gbl...
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


Nov 18 '05 #3

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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:e8*************@TK2MSFTNGP11.phx.gbl...
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

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:eq**************@TK2MSFTNGP11.phx.gbl...
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



Nov 18 '05 #4

P: n/a
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:
jc******@spartanmotors.com if you'd like to send directly to me.

Thanks,
-Rigs

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:Oo**************@tk2msftngp13.phx.gbl...
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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:e8*************@TK2MSFTNGP11.phx.gbl...
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

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:eq**************@TK2MSFTNGP11.phx.gbl...
> 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
>
>



Nov 18 '05 #5

P: n/a
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
"Rigs" <jc******@spartanmotors.com> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
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: jc******@spartanmotors.com if you'd like to send directly to me.

Thanks,
-Rigs

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:Oo**************@tk2msftngp13.phx.gbl...
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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:e8*************@TK2MSFTNGP11.phx.gbl...
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

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
> 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
>
> "Rigs" <jc******@spartanmotors.com> wrote in message
> news:eq**************@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> >
>
>



Nov 18 '05 #6

P: n/a
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.ControlToV alidate)

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
"Rigs" <jc******@spartanmotors.com> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
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: jc******@spartanmotors.com if you'd like to send directly to me.

Thanks,
-Rigs

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:Oo**************@tk2msftngp13.phx.gbl...
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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:e8*************@TK2MSFTNGP11.phx.gbl...
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

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
> 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
>
> "Rigs" <jc******@spartanmotors.com> wrote in message
> news:eq**************@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> >
>
>



Nov 18 '05 #7

P: n/a
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
"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:uv**************@TK2MSFTNGP09.phx.gbl...
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.ControlToV alidate)

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
"Rigs" <jc******@spartanmotors.com> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
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:
jc******@spartanmotors.com if you'd like to send directly to me.

Thanks,
-Rigs

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:Oo**************@tk2msftngp13.phx.gbl...
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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:e8*************@TK2MSFTNGP11.phx.gbl...
> 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
>
> "William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
> news:OD**************@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> > "Rigs" <jc******@spartanmotors.com> wrote in message
> > news:eq**************@TK2MSFTNGP11.phx.gbl...
> > > 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
> > >
> > >
> >
> >
>
>



Nov 18 '05 #8

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

"Rigs" <jc******@spartanmotors.com> wrote in message
news:OY*************@tk2msftngp13.phx.gbl...
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
"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:uv**************@TK2MSFTNGP09.phx.gbl...
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.ControlToV alidate)

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
"Rigs" <jc******@spartanmotors.com> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
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:
jc******@spartanmotors.com if you'd like to send directly to me.

Thanks,
-Rigs

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:Oo**************@tk2msftngp13.phx.gbl...
> 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
>
> "Rigs" <jc******@spartanmotors.com> wrote in message
> news:e8*************@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> > "William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message > > news:OD**************@TK2MSFTNGP11.phx.gbl...
> > > 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
> > >
> > > "Rigs" <jc******@spartanmotors.com> wrote in message
> > > news:eq**************@TK2MSFTNGP11.phx.gbl...
> > > > 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
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 18 '05 #9

P: n/a

ValidateEmptyText="true"

--
davious
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 17 '06 #10

P: n/a
Hi,

davious wrote:
ValidateEmptyText="true"
Answering to old posts without quoting the content makes it very
difficult to know what you're talking about.

Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 17 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.