ASP.NET控件之RequiredFieldValidator控件
阅读原文时间:2023年07月16日阅读:1

作用:对textbox或者其他输入框进行非空验证;

属性:ControlToValidate (选择要指向的控件)

ErrorMessage(错误,输入要显示的错误信息)

应用方法:

原型:

Demo

前端:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="非空验证.aspx.cs" Inherits="验证控件1.非空验证" %>





</div>  
    <asp:Label ID="Label1" runat="server" CssClass="auto-style1" Text="用户名:"></asp:Label>  
    <asp:Label ID="Label2" runat="server" CssClass="auto-style2" Text="密  码:"></asp:Label>  
    <asp:TextBox ID="txtUserName" runat="server" CssClass="auto-style3"></asp:TextBox>  
    <asp:TextBox ID="txtPwd" runat="server" CssClass="auto-style4"></asp:TextBox>  
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUserName" CssClass="auto-style5" ErrorMessage="\*请输入用户名" ForeColor="Red"></asp:RequiredFieldValidator>  
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPwd" CssClass="auto-style6" ErrorMessage="\*请输入密码" ForeColor="Red"></asp:RequiredFieldValidator>  
    <p>  
        &nbsp;</p>  
    <p>  
        &nbsp;</p>  
    <asp:Button ID="btnLogin" runat="server" style="z-index: 1; left: 389px; top: 259px; position: absolute; width: 63px; bottom: 360px" Text="登录" />  
    <asp:Button ID="btnSet" runat="server" OnClick="btnSet\_Click" style="z-index: 1; left: 559px; top: 262px; position: absolute; width: 64px; height: 21px" Text="重置" />  
</form>  



后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 验证控件1
{
public partial class 非空验证 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    }  
    //重置  
    protected void btnSet\_Click(object sender, EventArgs e)  
    {  
        txtPwd.Text = "";  
        txtUserName .Text ="";  
    }  
    //登录  
    protected void btnLogin\_Click(object sender, EventArgs e)  
    {

    }  
}  

}

小结

1、多多总结吧,万一哪天用到了呢···

感谢您的宝贵时间···

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章