使用CustomValidator控件
CustomValidator三处重要属性:
ControlToValidator
Text
ClientValidationFunction 客户端验证的客户端函数名
支持事件
ServerValidate -CustomValidator执行验证时引发
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void valComments_ServerValidate(object source, ServerValidateEventArgs args)
{
if (args.Value.Length > 10)
args.IsValid = false;
else args.IsValid = true;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Show CustomValidator</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblComents" Text="Comments:" AssociatedControlID="txtComments" runat="server" />
<br />
<asp:TextBox ID="txtComments" TextMode="MultiLine" Columns="30" Rows="5" runat="server" />
<asp:CustomValidator ID="valComments" ControlToValidate="txtComments" Text="(Comments must be less than 10 characters)"
runat="server" OnServerValidate="valComments_ServerValidate" />
<br />
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</div>
</form>
</body>
</html>
----------------------------------------
CustomValidator三处重要属性:
ControlToValidator
Text
ClientValidationFunction 客户端验证的客户端函数名
支持事件
ServerValidate -CustomValidator执行验证时引发
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void valComments_ServerValidate(object source, ServerValidateEventArgs args)
{
if (args.Value.Length > 10)
args.IsValid = false;
else args.IsValid = true;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Show CustomValidator</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblComents" Text="Comments:" AssociatedControlID="txtComments" runat="server" />
<br />
<asp:TextBox ID="txtComments" TextMode="MultiLine" Columns="30" Rows="5" runat="server" />
<asp:CustomValidator ID="valComments" ControlToValidate="txtComments" Text="(Comments must be less than 10 characters)"
runat="server" OnServerValidate="valComments_ServerValidate" />
<br />
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</div>
</form>
</body>
</html>
----------------------------------------