使用验证控件可以向服务器提交表单数据时验证表单内容,下面以RequiredFieldValidator和CompareValidator为例说明验证控件的用法
RequiredFieldValidator用来检查必填字段CompareValidator控件可以用来检查数据类型或者比较大小。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidationControl.aspx.cs" Inherits="WebApplication1.ValidationControl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type ="text/css" > .red { color:Red ; } </style> </head> <body> <form id="form1" runat="server"> <div> <fieldset > <legend title ="123">Product Order Form </legend> <asp:Label ID ="lblProductName" runat ="server" Text =" Product Name:" AssociatedControlID ="txtProductName"></asp:Label> <br /> <asp:TextBox ID ="txtProductName" runat ="server" ></asp:TextBox> <asp:RequiredFieldValidator ID ="reqProductName" ControlToValidate="txtProductName" runat ="server" Text ="(Require)" CssClass ="red " SetFocusOnError ="true" ></asp:RequiredFieldValidator> <br /> <asp:Label ID ="lblPrice" runat ="server" Text ="Price:" AssociatedControlID ="txtPrice" ></asp:Label> <asp:TextBox ID ="txtPrice" runat ="server" ></asp:TextBox> <asp:RequiredFieldValidator ID ="reqPrice" runat ="server" ControlToValidate="txtPrice" Text ="(Require)" CssClass ="red " SetFocusOnError ="true" ></asp:RequiredFieldValidator> <asp:CompareValidator ID ="comPrice" runat ="server" ControlToValidate ="txtPrice" Operator ="DataTypeCheck" Text ="Invalid Value" Type ="Currency" CssClass ="red " SetFocusOnError ="true" ></asp:CompareValidator> <br /> <asp:Label ID ="lblQty" runat ="server" Text ="Qty:" AssociatedControlID ="txtQty"></asp:Label> <asp:TextBox ID ="txtQty" runat ="server" ></asp:TextBox> <asp:CompareValidator ID ="comQty" runat ="server" ControlToValidate="txtQty" Text ="Invalid Value" CssClass ="red " Display ="Dynamic" Operator ="DataTypeCheck" Type ="Integer" SetFocusOnError ="true" ></asp:CompareValidator> <br /> <asp:Button ID ="txtSummit" runat ="server" Text ="提交" onclick="txtSummit_Click" /> </fieldset> <asp:Label ID ="lblResult" runat ="server" ></asp:Label> </div> </form> </body> </html>
public partial class ValidationControl : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void txtSummit_Click(object sender, EventArgs e) { if (Page.IsValid) { lblResult.Text = "Product Name:" + txtProductName.Text + "<br/>" + "Price:" + txtPrice.Text + "<br/>" + "Qty:" + txtQty.Text; } } }
例子说明:
1.验证控件ControlToValidate属性用于指定需要验证的控件;
2.验证控件的Text属性用于验证错误时显示错误信息,一般Text属性只是简单的文字,但是Text属性也支持html,比如<img src="error.gif" ali=""/>用一个图片代替简单的文字;
3.如果要验证信息显示红色,或者别的特殊样式,可以使用CssClass指定样式;
4.验证控件的 Display属性,Display属性有Dynamic、Static、None三个值,默认Static,
Display为Static时生成的错误信息如下
<span id="reqPrice" class="red " style="visibility: visible;">(Require)</span>
Display为Dynamic生成的错误信息为
<span id="reqPrice" class="red " style="display: none;">(Require)</span>
两者的区别是visibility: visible虽然隐藏了,但还是占据窗口空间,display: none不占窗口空间,我们应该设置Display为Dynamic这样验证信息后面的控件就不会被推倒右边
None 生成的标签如下,错误信息为空,所以不能用来显示错误提示
<span id="reqPrice" class="red " style="display: none;"></span>
5.验证控件默认在客户端和服务器端都进行验证,一些低端的浏览器不支持JavaScript,所以在服务器端进行验证能保证数据的有效性。可以使用EnableClientScript ="false"禁用客户端验证。
6.在带有验证控件的页面提交数据时需要检查 Page.IsValid属性,因为假如客户端验证不生效,服务器端虽然会进行验证,但是并不会阻止数据提交,只是简单的显示错误信息,如
protected void txtSummit_Click(object sender, EventArgs e) { if (Page.IsValid) { lblResult.Text = "Product Name:" + txtProductName.Text + "<br/>" + "Price:" + txtPrice.Text + "<br/>" + "Qty:" + txtQty.Text; } }
7.使用SetFocusOnError ="true"来让控件验证不通过时获取焦点,如果有几个验证控件同时设置,第一个控件或者焦点,因为同一个时间只能有一个控件获得焦点。
8.可以使用Page.Validators访问所有的验证控件,或者设置特殊样式。
Page_PreRender事件发生在所有控件事件之后,Page_load事件发生在所有控件事件之前
protected void Page_PreRender(object sender, EventArgs e) { foreach (BaseValidator c in Page.Validators) { if (c.IsValid) c.BackColor = System.Drawing.Color.Yellow; else c.BackColor = System.Drawing.Color.White; } }