https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.customvalidator?view=netframework-4.8

官方的demo使用的是ControlToValidate来关联被检查的控件。

OnServerValidate method

通过OnServerValidatef方法来进行服务端的验证。

Raises the ServerValidate event for the CustomValidator control.

Remarks

The ServerValidate event is raised when validation is performed on the server.

Raising an event invokes the event handler through a delegate. For more information, see Handling and Raising Events.

The OnServerValidate method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors

When overriding OnServerValidate(String) in a derived class, be sure to call the base class's OnServerValidate(String) method so that registered delegates receive the event.

Example

<asp:TextBox id="Text1"
runat="server" /> &nbsp;&nbsp; <asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
Display="Static"
ErrorMessage="Not an even number!"
ForeColor="green"
Font-Names="verdana"
Font-Size="10pt"
OnServerValidate="ServerValidation"
runat="server"/>
ServerValidation的实现,这段代码,可以直接写在后台的.cs文件中
<script runat="server">

      void ValidateBtn_OnClick(object sender, EventArgs e)
{ // Display whether the page passed validation.
if (Page.IsValid)
{ Message.Text = "Page is valid."; } else
{ Message.Text = "Page is not valid!"; } } void ServerValidation(object source, ServerValidateEventArgs args)
{ try
{ // Test whether the value entered into the text box is even.
int i = int.Parse(args.Value);
args.IsValid = ((i%2) == 0); } catch(Exception ex)
{ args.IsValid = false; } } </script>

另外还可以设定ValidationGroup来处理,让目标control和validator绑定为相同的group。

ClientValidationFunction property

Gets or sets the name of the custom client-side script function used for validation.

Remarks

Set this property to the name of the function that performs the client-side validation.

Because the client validation function runs on the target browser, the function must be written using a scripting language supported by the browser, such as JScript or VBScript.

This property cannot be set by themes or style sheet themes. For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

Example

  <asp:TextBox id="Text1"
runat="server" /> &nbsp;&nbsp; <asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage="Not an even number!"
ForeColor="green"
Font-Name="verdana"
Font-Size="10pt"
runat="server"/>
ClientValidate的逻辑
<script language="javascript">
function ClientValidate(source, arguments)
{
if (arguments.Value % 2 == 0 ){
arguments.IsValid = true;
} else {
arguments.IsValid = false;
}
}
</script>

ErrorMessage Property

Gets or sets the text for the error message displayed in a ValidationSummary control when validation fails.

The error message displayed in a ValidationSummary control when validation fails. The default value is an empty string (""), which indicates that this property is not set.

Remarks

When using a ValidationSummary control, use the ErrorMessage property to specify the text to display in the ValidationSummary control when validation fails for the current validation control.

To specify the text to display in the validation control itself, use the Text property.

Note

If you set the ErrorMessage property without setting the Text property, the value of the ErrorMessage property is also displayed in the validation control.

The value of this property, when set, can be saved automatically to a resource file by using a designer tool. For more information, see LocalizableAttribute and ASP.NET Globalization and Localization.

Text Property

Gets or sets the text displayed in the validation control when validation fails.

The text displayed in the validation control when validation fails. The default is an empty string (""), which indicates that this property is not set.

Remarks

Use the Text property to specify the text to display in a validation control when validation fails. You can also display a summary of all controls that fail validation in the page by using a ValidationSummary control. To specify the text to display in a ValidationSummary control, use the ErrorMessage property.

Display Property

Gets or sets the display behavior of the error message in a validation control.

One of the ValidatorDisplay values. The default value is Static.

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.validatordisplay?view=netframework-4.8

Dynamic 2

Validator content dynamically added to the page when validation fails.

None 0

Validator content never displayed inline.

Static 1

Validator content physically part of the page layout.

The ValidatorDisplay enumeration represents the different display behaviors of error messages in validation controls.

None specifies that you only want to display the error message in a ValidationSummary control. The error message will not display in the validation control.

Static specifies that you don't want the layout of the Web page to change when validator controls display error messages. Space on the page is allocated for the error messages when displaying the page. The validator contents are physically part of the page; therefore, multiple validators for the same input control must occupy different locations on the page.

Dynamic specifies that you want to dynamically place error messages on the Web page when validation fails. Space for the validation content is not allocated on the page; therefore, the page dynamically changes to display the error message. This allows multiple validators to share the same physical location on the page. In order to keep the page layout from changing when an error message is displayed, the HTML element containing the validator must be sized large enough to accommodate the maximum size of the validator.

扩展阅读

https://www.c-sharpcorner.com/UploadFile/manas1/displaying-dynamic-error-message-in-Asp-Net-custom-validator/

Remarks

Use the CustomValidator control to provide a user-defined validation function for an input control. The CustomValidator control is a separate control from the input control it validates, which allows you to control where the validation message is displayed.

Validation controls always perform validation on the server. They also have complete client-side implementation that allows script-enabled browsers (such as Microsoft Internet Explorer 4.0 and later) to perform validation on the client. Client-side validation enhances the validation process by checking user input before it is sent to the server. This allows errors to be detected on the client before the form is submitted, thus avoiding the round trip of information necessary for server-side validation.

To create a server-side validation function, provide a handler for the ServerValidate event that performs the validation. The string from the input control to validate can be accessed by using the Value property of the ServerValidateEventArgs object passed into the event handler as a parameter. The result of the validation is then stored in the IsValid property of the ServerValidateEventArgs object.

To create a client-side validation function, first add the server-side validation function described earlier. Next, add the client-side validation script function to the ASP.NET (.aspx) page.

f you are using JScript, the function must be in this form:

 
function ValidationFunctionName(source, arguments)

The source parameter is a reference to the <span> element rendered for the CustomValidator control. This allows you to programmatically control the <span> tag, such as modifying the InnerHtml attribute. The arguments parameter is an object with two properties: Value and IsValid. This parameter allows you to get the value of the control to validate and to indicate whether the value is valid based on your custom validation routine.

Use the ClientValidationFunction property to specify the name of the client-side validation script function associated with the CustomValidator control. Because the script function is executed on the client, the function must be in a language that the target browser supports, such as VBScript or JScript.

Button的客户端验证

https://stackoverflow.com/questions/6897196/firing-both-onclientclick-and-client-side-validation-from-validation-control-on

Change OnClientClick from return isValidTracker() to if (!isValidTracker()) return false;

webform CustomValidator的更多相关文章

  1. WebForm获取GET或者POST参数到实体的转换,ADO.NET数据集自动转换实体

    最近在修改维护以前的webform项目(维护别人开发的.....)整个aspx没有用到任何的控件,这个我也比较喜欢不用控件所以在提交信息的时候需要自己手动的去Request.QueryString[] ...

  2. Webform:Application、ViewState对象的用法

    Application Application对象的作用范围是整个全局,也就是说对所有用户都有效.它在整个应用程序生命周期中都是有效的,类似于使用全局变量一样,所以可以在不同页面中对它进行存取.它和S ...

  3. webform开发基础

    ASP.NET WebForm C/S(Client/Server):客户端服务器 B/S(Browser/Server):浏览器服务器 C/S和B/S的区别: 首先必须强调的是C/S和B/S并没有本 ...

  4. 编写轻量ajax组件01-对比webform平台上的各种实现方式

    前言 Asp.net WebForm 和 Asp.net MVC(简称MVC) 都是基于Asp.net的web开发框架,两者有很大的区别,其中一个就是MVC更加注重http本质,而WebForm试图屏 ...

  5. 自己写的一个Pager分页组件,WebForm,Mvc都适用

    我一说写这个功能的时候,好多人估计有疑问.分页功能网上多的是,搜一个不就行了,你这样不是浪费时间么.你说这句话的时候,我是比较信的,首先自己写一些东西是很耗时,有这些时间又能多打几盘LOL了.但是我觉 ...

  6. WebForm业务系列-会员功能

    用了这么久的webform,还记得刚开始根本不知道程序要写成什么样,只知道功能实现了就行,有很多现实的问题都没考虑到.所以程序改了又改,最后连自己做的什么都不知道了.所以,现在来总结一下. 会员功能 ...

  7. WebForm基础--2016年12月27日

    C/S:winform WPF 数据是存在其它的电脑上或服务器上需要从服务器上下载相应的数据,在本地电脑上的客户端里进行加工 数据加工的过程是在用户电脑上执行,会对用户的电脑配置有所要求 B/S:AS ...

  8. 在mvc里面有htmlhelper方法,在webform里面有什么?

    终于是找到原来在webform里面已经提供了htmlcontrol这样的控件,可以直接拿来用.以前一直在想mvc有htmlhelper,webform里面不能用,其实是webform里面已经有了. 例 ...

  9. Webform Application传值 ViewState

    Application:所有的会话共享一个Application空间,任何一个人改变Application的内容,其他人都会发现被改变了.Application中的内容不会被自动释放 存放位置:服务端 ...

随机推荐

  1. c#获取QQ音乐当前播放的歌曲名

    在网上找了很久,没找到方法,自己尝试着做,还是做出来了,很简单,就几句代码. Process[] ps = Process.GetProcessesByName("QQmusic" ...

  2. 吉哥系列故事——完美队形II---hdu4513(最长回文子串manacher)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4513 题意比最长回文串就多了一个前面的人要比后面的人低这个条件,所以在p[i]++的时候判断一下s[i ...

  3. linux伙伴系统接口alloc_page分析1

    在内核中分配内存,最后要通过伙伴系统接口进行实际物理页面的分配,一个重要的接口便是alloc_page.本文介绍下alloc_page的主要流程,各个部分的执行.主要包含正常分配流程,当页面不足的时候 ...

  4. 类的super

    我们经常在类的继承当中使用super(), 来调用父类中的方法.例如下面: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A:     def func(self):   ...

  5. 【opencv入门篇】 10个程序快速上手opencv【下】

    导言:本系列博客目的在于能够在vs快速上手opencv,理论知识涉及较少,大家有兴趣可以查阅其他博客深入了解相关的理论知识,本博客后续也会对图像方向的理论进一步分析,敬请期待:) 上篇传送:http: ...

  6. setup.py

    from distutils.core import setup # 使用说明 # 执行以下命令 # python3 setup.py build # python3 setup.py sdist(这 ...

  7. jQuery -&gt; 使用andSelf()来包括之前的选择集

    版权声明:本文为博主原创文章.转载请注明出处 https://blog.csdn.net/FeeLang/article/details/26254793 当我们使用Destructive Metho ...

  8. lock,Monitor,Mutex的区别

    lock和Monitor的区别 一.lock的底层本身是Monitor来实现的,所以Monitor可以实现lock的所有功能. 二.Monitor有TryEnter的功能,可以防止出现死锁的问题,lo ...

  9. U盘安装CentOS7笔记

    准备工具: 8G左右U盘; 最新版UltraISO; CentOS7光盘镜像; CentOS7的镜像文件可以在网易的开源镜像站或者阿里云的开源镜像站下载,地址分别是:http://mirrors.16 ...

  10. JavaScript和jQuery的学习

    还有12天就要回学校了,我的假期计划还能实现吗?在这12天里,需要把JavaScript和jQuery学完.我知道这两个技术对于前端网页开发非常重要.前期把HTML和CSS学完了,学的不是特别深,只是 ...