Элемент управления RangeValidator проверяет, находится ли значение в определенном диапазоне.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
-
<
meta
charset
=
"utf-8"
/>
-
<
title
></
title
>
-
<
style
>
-
.a {
-
float: left;
-
padding: 5px;
-
}
-
</
style
>
</
head
>
<
body
>
-
<
form
id
=
"form1"
runat
=
"server"
>
-
<
div
>
-
<
div
class
=
"a"
>
-
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text
=
"Введите число от 0 до 10"
></
asp:Label
>
-
</
div
>
-
<
div
class
=
"a"
>
-
<
asp:TextBox
ID
=
"TextBox1"
runat
=
"server"
></
asp:TextBox
>
-
</
div
>
-
<
div
class
=
"a"
>
-
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator1"
runat
=
"server"
ErrorMessage
=
"RequiredFieldValidator"
ControlToValidate
=
"TextBox1"
ForeColor
=
"Red"
>Заполните поле</
asp:RequiredFieldValidator
>
-
<
br
/>
-
<
asp:RangeValidator
ID
=
"RangeValidator1"
runat
=
"server"
ErrorMessage
=
"RangeValidator"
ControlToValidate
=
"TextBox1"
ForeColor
=
"Red"
MaximumValue
=
"10"
MinimumValue
=
"0"
Type
=
"Integer"
>Значение за пределами диапазона</
asp:RangeValidator
>
-
</
div
>
-
<
br
/>
-
<
br
/>
-
<
br
/>
-
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"OK"
OnClick
=
"Button1_Click"
/>
-
<
br
/>
-
<
br
/>
-
<
asp:Label
ID
=
"Label2"
runat
=
"server"
Text
=
""
></
asp:Label
>
-
</
div
>
-
</
form
>
</
body
>
</
html
>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
meta
charset
=
"utf-8"
/>
<
title
></
title
>
<
style
>
.a {
float: left;
padding: 5px;
}
</
style
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
div
class
=
"a"
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text
=
"Введите число от 0 до 10"
></
asp:Label
>
</
div
>
<
div
class
=
"a"
>
<
asp:TextBox
ID
=
"TextBox1"
runat
=
"server"
></
asp:TextBox
>
</
div
>
<
div
class
=
"a"
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator1"
runat
=
"server"
ErrorMessage
=
"RequiredFieldValidator"
ControlToValidate
=
"TextBox1"
ForeColor
=
"Red"
>Заполните поле</
asp:RequiredFieldValidator
>
<
br
/>
<
asp:RangeValidator
ID
=
"RangeValidator1"
runat
=
"server"
ErrorMessage
=
"RangeValidator"
ControlToValidate
=
"TextBox1"
ForeColor
=
"Red"
MaximumValue
=
"10"
MinimumValue
=
"0"
Type
=
"Integer"
>Значение за пределами диапазона</
asp:RangeValidator
>
</
div
>
<
br
/>
<
br
/>
<
br
/>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"OK"
OnClick
=
"Button1_Click"
/>
<
br
/>
<
br
/>
<
asp:Label
ID
=
"Label2"
runat
=
"server"
Text
=
""
></
asp:Label
>
</
div
>
</
form
>
</
body
>
</
html
>
Default.aspx.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public
partial
class
_Default : System.Web.UI.Page {
-
protected
void
Page_Load(
object
sender, 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>
-
*/
-
}
-
protected
void
Button1_Click(
object
sender, EventArgs e) {
-
if
(Page.IsValid) {
-
Label2.Text =
"Введенное число "
+ TextBox1.Text;
-
}
-
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public
partial
class
_Default : System.Web.UI.Page {
protected
void
Page_Load(
object
sender, 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>
*/
}
protected
void
Button1_Click(
object
sender, EventArgs e) {
if
(Page.IsValid) {
Label2.Text =
"Введенное число "
+ TextBox1.Text;
}
}
}