且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义验证程序仅在数据存在时触发OnServerValidate

更新时间:2023-02-26 22:14:26

CustomValidator以及所有其他内置验证器仅在存在

数据时触发。即.Text.Length!= 0,否则验证返回

true。


我能想到的两个选项,我相信还有更多选项:


1.从BaseValidator派生并覆盖EvaluateIsValid方法。无论控件中是什么,每次都会触发这个



2.从CustomValidator派生并覆盖EvaluateIsValid方法。


第二种方法可能就是你想要的。


//这是有点代码,它应该可以工作,但我真的没有测试它。

protected override bool EvaluateIsValid()

{

string s = GetControlValidationValue(this.ControlToValidate);

返回this.OnServerValidate (s);

}


HTH,


bill


" Rigs" < JC ****** @ spartanmotors.com>在消息中写道

news:eq ************** @ TK2MSFTNGP11.phx.gbl ...
>
我有一个带有自定义验证器的文本框,它使用该文本框的
OnServerValidate方法。这样可以正常工作,但是在单击提交按钮后,当该文本框中存在数据时,只执行
的方法。如果我单击提交按钮并且文本框中没有数据,则不会触发OnServerValidate方法。

我希望每次都执行OnServerValidate方法单击
Submit按钮。我也愿意接受有关替代方法的建议
来验证控件。我无法使用RequiredFieldValidator
,因为Web表单中其他文本框中的数据确定
问题中的文本框是否应包含数据。它有点复杂,因此使用
自定义验证器。

如果有人有任何建议,我很乐意听到它们。

谢谢,
-Rigs



比尔,


感谢您的建议。不幸的是,我仍然是一个绿色的ASP

..NET。您能否进一步扩展您的回答中的意思?我怎么能实现你的建议呢?任何其他评论将不胜感激。


谢谢,

-Rigs


" William F. Robertson,小" < WF ********* @ kpmg.com>在消息中写道

新闻:OD ************** @ TK2MSFTNGP11.phx.gbl ...
CustomValidator以及所有其他内置验证器仅在数据存在时触发
。即.Text.Length!= 0,否则验证返回
为真。

我能想到的两个选项,我相信还有更多选择:
> 1.从BaseValidator派生并覆盖EvaluateIsValid方法。
无论控件中是什么,这都会触发。
2.从CustomValidator派生并覆盖EvaluateIsValid方法。

第二种方法可能就是你想要去的for。

//这是有点代码,它应该可以工作,但我真的没有测试它。
受保护的覆盖bool EvaluateIsValid()
{
string s = GetControlValidationValue(this.ControlToValidate);
返回this.OnServerValidate(s);
}

HTH,

账单

Rigs < JC ****** @ spartanmotors.com>在消息中写道
新闻:eq ************** @ TK2MSFTNGP11.phx.gbl ...

>我有一个带有自定义验证器的文本框,该文本框利用该文本框的


OnServerValidate

方法。这样可以正常工作,但只有


在单击Submit按钮后该文本框中存在数据时才执行

。如果
我单击提交按钮并且文本框中没有数据,则不会触发OnServerValidate方法。

我希望每次都执行OnServerValidate方法


单击提交

按钮。我也愿意接受有关替代
方法的建议来验证控件。我不能使用RequiredFieldValidator


因为

web表单中其他文本框中的数据确定

中的文本框是否

应该包含数据。这有点复杂,因此使用


> blockquote class =post_quotes>自定义验证器。

如果有人有任何建议,我会爱听取他们的意见。

谢谢,
-Rigs




我假设您是用C#编写的。


您将此行添加到您的aspx页面顶部(不是代码隐藏)


<%@ Register TagPrefix =" myControl"命名空间= QUOT; tempasp&QUOT;装配= QUOT; tempasp&QUOT; %>


这是要验证的控件。 dascweb:前缀是我自己的东西,它

通常是asp:TextBox,但这真的没关系

< dascweb:TextBoxEx ID =" txtPhone&QUOT; Runat =" server"

IsRequired =" False">< / dascweb:TextBoxEx>


这是验证控件。

< mycontrol:MyCustomValidator id =" valCustom" runat =" server"

ControlToValidate =" txtPhone" ErrorMessage ="消息在

错误。">< / mycontrol:MyCustomValidator>


现在在解决方案的某个地方你需要定义控制。


我的解决方案是tempasp,所以tempasp中所有文件的命名空间。


这是我的控制代码:

公共类MyCustomValidator:CustomValidator

{

protected override bool EvaluateIsValid()

{

string s = this.GetControlValidationValue(this.ControlToValidate);

返回this.OnServerValidate(s);

}


}


这将绕过内置的

CustomValidator控件的null / String.Empty检查并触发你的ServerValidate事件

希望收到。


如果这仍然无法帮助您发布电子邮件地址,我可以向您发送一些文件

。 />

HTH,


账单


" Rig S&QUOT; &LT; JC ****** @ spartanmotors.com&GT;在消息中写道

新闻:e8 ************* @ TK2MSFTNGP11.phx.gbl ...
比尔,

感谢您的建议。不幸的是,我仍然有点用.NET
.NET。您能否进一步扩展您的回答中的意思?
我将如何实施您的建议?任何其他评论将不胜感激。

谢谢,
-Rigs

William F. Robertson,Jr。 &LT; WF ********* @ kpmg.com&GT;在消息中写道
新闻:OD ************** @ TK2MSFTNGP11.phx.gbl ...
CustomValidator以及所有其他内置验证器仅在
数据存在时触发。即.Text.Length!= 0,否则验证返回
为真。

我能想到的两个选项,我相信还有更多选择:
> 1.从BaseValidator派生并覆盖EvaluateIsValid方法。


无论控件中是什么,每次都会触发


2.来自CustomValidator和覆盖EvaluateIsValid方法。

第二种方法可能是你想要的。

//这是有点代码,它应该有用,但我真的没有'测试它。
受保护的覆盖bool EvaluateIsValid()
{/ /> string s = GetControlValidationValue(this.ControlToValidate);
返回this.OnServerValidate(s);
HTH,

账单

Rigs &LT; JC ****** @ spartanmotors.com&GT;在消息中写道
新闻:eq ************** @ TK2MSFTNGP11.phx.gbl ...

>我有一个带有自定义验证器的文本框,该文本框利用该文本框的


OnServerValidate

方法。这样可以正常工作,但只有


执行

时,如果单击提交按钮后该文本框中存在数据,则


如果单击提交按钮并且文本框中没有数据,
OnServerValidate方法不会触发。

我希望OnServerValidate方法每次都执行


单击提交

按钮。我也对有关验证控件的替代方法的建议持开放态度。我不能使用RequiredFieldValidator


因为

web表单中其他文本框中的数据确定

中的文本框是否

应该包含数据。它有点复杂,因此
自定义验证器的使用




如果有人有任何建议,我会爱听取他们的意见。

谢谢,
-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

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



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




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