Элемент управления CustomValidator проверяет значение с помощью пользовательской функции.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server">-
<metacharset="utf-8"/> -
<title></title> -
<style> -
td { -
padding: 5px; -
} -
.b { -
text-align: right; -
} -
.c { -
text-align: center; -
} -
</style> -
<script> -
/*source - span, в который выводиться сообщение об ошибке*/ -
/*arguments - дополнительные параметры*/ -
/*Value - значение поля ввода*/ -
/*IsValid - корректность введенных данных в поле ввода*/ -
function funValidation(source, arguments) { -
if(arguments.Value % 2 == 0) { -
arguments.IsValid = true; -
} -
else { -
arguments.IsValid = false; -
source.innerHTML = "Это не четное число"; -
} -
} -
</script> </head><body>-
<formid="form1"runat="server"> -
<div> -
<table> -
<tr> -
<td> -
<asp:LabelID="Label1"runat="server"Text="Введите чётное число"></asp:Label> -
</td> -
<td> -
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox> -
</td> -
<td> -
<asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"ErrorMessage="RequiredFieldValidator"ControlToValidate="TextBox1"ForeColor="Red">Заполните поле</asp:RequiredFieldValidator> -
</td> -
<td> -
<asp:CustomValidatorID="CustomValidator1"runat="server"ErrorMessage="CustomValidator"ControlToValidate="TextBox1"ClientValidationFunction="funValidation"ForeColor="Red"OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator> -
</td> -
</tr> -
<tr> -
<td> -
<asp:ButtonID="Button1"runat="server"Text="OK"OnClick="Button1_Click"/> -
</td> -
<td></td> -
<td></td> -
<td></td> -
</tr> -
</table> -
<br/> -
<br/> -
<asp:LabelID="Label2"runat="server"Text=""></asp:Label> -
</div> -
</form> </body></html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><metacharset="utf-8"/><title></title><style>td {padding: 5px;}.b {text-align: right;}.c {text-align: center;}</style><script>/*source - span, в который выводиться сообщение об ошибке*//*arguments - дополнительные параметры*//*Value - значение поля ввода*//*IsValid - корректность введенных данных в поле ввода*/function funValidation(source, arguments) {if(arguments.Value % 2 == 0) {arguments.IsValid = true;}else {arguments.IsValid = false;source.innerHTML = "Это не четное число";}}</script></head><body><formid="form1"runat="server"><div><table><tr><td><asp:LabelID="Label1"runat="server"Text="Введите чётное число"></asp:Label></td><td><asp:TextBoxID="TextBox1"runat="server"></asp:TextBox></td><td><asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"ErrorMessage="RequiredFieldValidator"ControlToValidate="TextBox1"ForeColor="Red">Заполните поле</asp:RequiredFieldValidator></td><td><asp:CustomValidatorID="CustomValidator1"runat="server"ErrorMessage="CustomValidator"ControlToValidate="TextBox1"ClientValidationFunction="funValidation"ForeColor="Red"OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator></td></tr><tr><td><asp:ButtonID="Button1"runat="server"Text="OK"OnClick="Button1_Click"/></td><td></td><td></td><td></td></tr></table><br/><br/><asp:LabelID="Label2"runat="server"Text=""></asp:Label></div></form></body></html>
Default.aspx.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;publicpartialclass_Default : System.Web.UI.Page {-
protectedvoidPage_Load(objectsender, EventArgs e) { -
/*---При возникновении ошибки---*/ -
/*В WebForms для режима UnobtrusiveValidationMode требуется сопоставление -
ScriptResourceMapping для "jquery". Добавьте сопоставление -
ScriptResourceMapping с именем jquery (с учетом регистра)*/ -
/*---добавьте код---*/ -
Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None; -
/*---или добавьте строку в файл Web.config---*/ -
/* -
<appSettings> -
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> -
</appSettings> -
*/ -
} -
protectedvoidButton1_Click(objectsender, EventArgs e) { -
if(Page.IsValid) { -
Label2.Text ="Вы ввели четное число : "+ TextBox1.Text; -
} -
} -
protectedvoidCustomValidator1_ServerValidate(objectsource, ServerValidateEventArgs args) { -
intvalue = 0; -
if(Int32.TryParse(args.Value,outvalue)) { -
if(value % 2 == 0) { -
args.IsValid =true; -
} -
else{ -
args.IsValid =false; -
(sourceasLabel).Text ="Это не четное число"; -
} -
} -
else{ -
args.IsValid =false; -
(sourceasLabel).Text ="Введите четное число"; -
} -
} }
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;publicpartialclass_Default : System.Web.UI.Page {protectedvoidPage_Load(objectsender, EventArgs e) {/*---При возникновении ошибки---*//*В WebForms для режима UnobtrusiveValidationMode требуется сопоставлениеScriptResourceMapping для "jquery". Добавьте сопоставлениеScriptResourceMapping с именем jquery (с учетом регистра)*//*---добавьте код---*/Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;/*---или добавьте строку в файл Web.config---*//*<appSettings><add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /></appSettings>*/}protectedvoidButton1_Click(objectsender, EventArgs e) {if(Page.IsValid) {Label2.Text ="Вы ввели четное число : "+ TextBox1.Text;}}protectedvoidCustomValidator1_ServerValidate(objectsource, ServerValidateEventArgs args) {intvalue = 0;if(Int32.TryParse(args.Value,outvalue)) {if(value % 2 == 0) {args.IsValid =true;}else{args.IsValid =false;(sourceasLabel).Text ="Это не четное число";}}else{args.IsValid =false;(sourceasLabel).Text ="Введите четное число";}}}