RequiredFieldValidator控件验证使用

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldValidator.aspx.cs" Inherits="RequiredFieldValidator.RequiredFieldValidator" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <h1>
        Bug Reporter
    </h1>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td align="center" colspan="3">
                <asp:Label ID="Label1" runat="server" Text="please report your bug here" ></asp:Label>
            </td>        
        </tr>

以上包括一个控件Label。


        <tr>
            <td>book</td>
            <td>

                    <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>--please pick A book--</asp:ListItem>
                    <asp:ListItem>learning</asp:ListItem>
                </asp:DropDownList>

            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="please choose a book"
                     ControlToValidate="DropDownList1" Display="Static" InitialValue="--please pick A book--">
                </asp:RequiredFieldValidator>
            </td>
        </tr>

        以上包括两个控件 一个为DropDownList,一个为RequiredFieldValidatorRequiredFieldValidator最重要的属性就是ControlToValidateErrorMessage,前者用于识别要验证的控件,后者用于包含一个文本信息,当控件在列表中没有被选中时该信息就会显示出来,或者选中值与InitialValue属性值相同时也会显示这个信息。

      RequiredFieldValidator还有一个Display属性,被设置为"static",该值告诉ASP.NET无论显示信息与否,都会为该控件在页面中分配显示信息的空间。如果设置为"Dynamic",只有在错误信息要显示时才分配空间。

 


        <tr>
            <td>

                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

            </td>
        </tr>
    </table>
    </form>
</body>
</html>

运行该页面然后不做任何操作,直接点击button按钮,每个被验证的控件会被检查,然后显示出错误信息。

 

一旦发生验证,当服务器端有任何验证发生时ASP.NET会回发到服务器。Page的IsValid属性值取决于每个需要验证的控件是否有效,如果验证通过,

Page的IsValid会被设置成true。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值