473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Custom Validator Only Fires OnServerValidate When Data Exists

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
10 5416
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
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
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
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
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
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
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
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

ValidateEmptyText="true"

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

Nov 17 '06 #10
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael ALbanese | last post by:
I am developing a telephone directory for my company. I have Fist Name, Last Name, Department and Phone Number as fields. A user can search by entering data into any one, or combination of these...
2
by: Mike P | last post by:
I have a Custom validator that has ControlToValidate set to a textbox that also has a Required and RegularExpression validator. When I run the webpage on my machine the custom validator...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
2
by: Alan Silver | last post by:
Hello, I have a custom validator on my page, and have the server-side code working fine. I want to add a client-side funtion as well, but am not sure how to wire it in so that it works with the...
3
by: Andy | last post by:
Hi folks, I have a customvalidator control that works properly if it isn't contained in an ASP:TABLE. But, when I place it inside an ASP:TABLE, I find that _ServerValidate doesn't get fired,...
3
by: Rich Squid | last post by:
Hello Here's my basic problem: On my asp.net form page I have a DetailsView (default mode=edit) bound to a AccessDataSource control. Users can successfuly update a databound template field,...
8
by: Radu | last post by:
Hi. I have an ASP control on my page: <asp:Calendar ID="calStart" ................ Etc </asp:Calendar> and I have a Custom Validator defined as <asp:CustomValidator
2
by: tinariver | last post by:
I am using the onServerValidate of CutomValidator. I have got the Calender control and a text control. When user pick the date from calender conrol and value goes in the text box. Now I have to...
1
by: Steveaux | last post by:
Hi, I'm new to ASP.Net, so this may be something simple that I forgot. I have a form where a person can create a login. I'm doing the processing on this myself. The form has a plethora of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.