关于RequireFieldValidator控件

来源:互联网 发布:译码器和数据选择器 编辑:程序博客网 时间:2024/05/29 05:53

1、RequireFieldValidator控件是验证相应的输入框是不是为空的验证控件,但是,如果设置了ErrorMessage属      性,一旦输入框为空,无论你点击了页面的那个位置,都会有错误提示,而且,Page.IsValid属性的值只能取True,没有办法取得False值,好像Page.IsValid属性的false值被前面的ErrorMessage属性的值给屏蔽掉了一样,如果输入框为空时,页面都没有办法刷新,不能起作用,自己感觉这样子用起来可能能够达到验证的目的,输入错误时页面不刷新,可能能够减少服务器的负担,但是给人的感觉非常不友好。

注:1、Page.IsValid属性:
              获取一个值,该值指示页验证是否成功
             C#
             public bool IsValid { get; }

示例代码如下:

前台代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="NotNullValidate.aspx.cs" Inherits="_Default" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server" Height="310px" Width="151px">
            &nbsp;<asp:TextBox ID="txtInputBox" runat="server" Width="134px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="rfInputBox" runat="server" ControlToValidate="txtInputBox"></asp:RequiredFieldValidator>&nbsp;
            <asp:Button ID="btnValidate" runat="server" OnClick="btnValidate_Click" Text="测试验证 " />
            <asp:Label ID="lblValidate" runat="server" Text="Label" Width="144px"></asp:Label></asp:Panel>
   
    </div>
    </form>
</body>
</html>

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblValidate.Visible = false;
    }
    protected void btnValidate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            lblValidate.Text = "用户输入验证合法";
            lblValidate.Visible = true;
        }
        else
        {
            lblValidate.Text = "用户输入验证失败";
            lblValidate.Visible = false;
        }
    }
}

原创粉丝点击