ASP.NET中 RequiredFieldValidator(非空验证)的使用

1.A,运行效果返回顶部
登录
RequiredFieldValidator:非空验证

重要的属性:
1,ControlToValidate:要验证的控件
2,ErrorMessage:错误提示信息
 
用户账号:      请输入用户名 
密    码:      请输入密码 
           
 
1.B,源代码返回顶部
/Default.aspx
复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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>
    <h5>登录h5>
    RequiredFieldValidator:非空验证
    <hr />
        <br />
    重要的属性:<br />
    1,ControlToValidate:要验证的控件<br />
    2,ErrorMessage:错误提示信息<br />
 <hr />
    用户账号:<asp:TextBox ID="txtUsername" runat="server">asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="txtUsername" ErrorMessage="请输入用户名">asp:RequiredFieldValidator>
        <br />    码:<asp:TextBox ID="txtUserpass" 
            runat="server" TextMode="Password">asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
            ControlToValidate="txtUserpass" ErrorMessage="请输入密码">asp:RequiredFieldValidator>
        <br />
  
        <asp:Button ID="btnReset" runat="server" Text="重置" onclick="btnReset_Click" />
  
        <asp:Button ID="btnSubmit" runat="server" Text="登录" />
        <br />
    div>
    form>
body>
html>
复制代码
/Default.aspx.cs
复制代码
using System;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        
        //清空文本框
        //string.Empty 空对象
        txtUsername.Text = string.Empty;
        txtUserpass.Text = string.Empty;
    }
}
复制代码
1.C,资源下载返回顶部

http://files.cnblogs.com/ylbtech/WebForm-BasicControl-Validator.rar


你可能感兴趣的:(ASP.Net)