• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

编码实现 ASP.NET验证控件 验证逻辑和思想

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

编码实现>ASP.NET验证控件>验证逻辑和思想

指定输入规则,验证用户输入的数据。

输入规则:非空数据,数据值比较,符合正则表达式。

验证顺序:非空数据,转换为正确的数据类型。

 

编码实现>ASP.NET验证控件>非空验证

结果: 我们使用两个RequiredFieldValidator控件,一个用来要求用户必须在下拉框选择选项,另一个要用户必须在文本框输入数据,否则,单击“开始查询”按钮,有出错信息。

过程:章立民的《ASP.NET3.5开发范例精讲精析基于C# 》,数据检验的利器:验证控件 网页范例1

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

<!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 id="Head1" runat="server">
    
<title>验证控件 RequiredFieldValidator 使用示范</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        请选择部门:
        
<asp:DropDownList ID="Department_DropDownList" runat="server" 
            AppendDataBoundItems
="True" DataSourceID="Department_SqlDataSource" 
            DataTextField
="部门" DataValueField="部门">
            
<asp:ListItem Selected="True">【--请选择部门--】</asp:ListItem>
        
</asp:DropDownList>
        
        
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate
="Department_DropDownList" ErrorMessage="" Text = "您必须选择一个部门!" 
            InitialValue
="【--请选择部门--】">您必须选择一个部门!</asp:RequiredFieldValidator>                    
            
        
<asp:SqlDataSource ID="Department_SqlDataSource" runat="server" 
            ConnectionString
="<%$ ConnectionStrings:chtNorthwind %>" 
            SelectCommand
="SELECT DISTINCT [部门] FROM [飞狐工作室]"></asp:SqlDataSource>
        
<br />
        请输入城市:
        
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox> 
        
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtCity" ErrorMessage="" Text="您必须输入一个城市的名称"></asp:RequiredFieldValidator>
        
<br />
        
<asp:Button ID="btnGo" runat="server" Text="开始查询" />       
        
<asp:GridView ID="FoxStudio_GridView" runat="server" AllowPaging="True" 
            AutoGenerateColumns
="False" BackColor="#DEBA84" BorderColor="#DEBA84" 
            BorderStyle
="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" 
            DataSourceID
="FoxStudio_SqlDataSource" PageSize="3">
            
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            
<Columns>
                
<asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名">
                    
<ItemStyle Width="80px" />
                
</asp:BoundField>
                
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址">
                    
<ItemStyle Width="120px" />
                
</asp:BoundField>
                
<asp:BoundField DataField="自传" HeaderText="自传" SortExpression="自传" />
            
</Columns>
            
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        
</asp:GridView>
        
<asp:SqlDataSource ID="FoxStudio_SqlDataSource" runat="server" 
            ConnectionString
="<%$ ConnectionStrings:chtNorthwind %>" 
            SelectCommand
="SELECT [姓名], [家庭住址], [自传] FROM [飞狐工作室] WHERE (([部门] = @部门) AND ([家庭住址] LIKE '%' + @家庭住址 + '%'))">
            
<SelectParameters>
                
<asp:ControlParameter ControlID="Department_DropDownList" Name="部门" 
                    PropertyName
="SelectedValue" Type="String" />
                
<asp:ControlParameter ControlID="txtCity" Name="家庭住址" PropertyName="Text" 
                    Type
="String" />
            
</SelectParameters>
        
</asp:SqlDataSource>
    
</div>
    
</form>
</body>
</html>


编码实现>ASP.NET验证控件>输入的值比较

描述: 使用两个RequiredFieldValidator控件限制用户必须输入数据。CompareValidator控件限制用户输入的数据必须大于或等于10000,否则出错。当前薪资必须大于起薪。

过程:章立民的《ASP.NET3.5开发范例精讲精析基于C# 》,数据检验的利器:验证控件 网页范例2

 

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

<!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 id="Head1" runat="server">
    
<title>示范如何综合运用 RequiredFieldValidator 与 CompareValidator 控件来执行验证作业</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<table>
            
<tr>
                
<td style="text-align:right;">
                    起薪:
                
</td>
                
<td style="text-align:left;">
                    
<asp:TextBox ID="txtHireSalary" runat="server"></asp:TextBox>
                    
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtHireSalary"
                        ErrorMessage
="txtHireSalary" Text="您必须输入起薪!" Display="Dynamic" 
                        SetFocusOnError
="True"></asp:RequiredFieldValidator>
                    
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="" ControlToValidate="txtHireSalary"
                        ValueToCompare
="10000" Operator="GreaterThanEqual" Type="Currency" 
                        Display
="Dynamic" SetFocusOnError="True">起薪必须大于或等于 10000 
                    元!
</asp:CompareValidator>
                
</td>
            
</tr>
            
<tr>
                
<td style="text-align:right;">
                    当前薪资:
                
</td>
                
<td style="text-align:left;">
                    
<asp:TextBox ID="txtCurSalary" runat="server"></asp:TextBox>
                    
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtCurSalary"
                        ErrorMessage
="txtHireSalary" Text="您必须输入当前薪资!" Display="Dynamic" 
                        SetFocusOnError
="True"></asp:RequiredFieldValidator>
                
</td>
            
</tr>
            
<tr>
                
<td colspan="2">
                    
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToCompare="txtCurSalary"
                        ControlToValidate
="txtHireSalary" Display="Dynamic" Operator="LessThanEqual"
                        Type
="Currency" SetFocusOnError="True">起薪必须小于或等于当前薪资!</asp:CompareValidator>
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
<asp:Button ID="btnOk" runat="server" Text="确定" />
                
</td>
                
<td>
                    
&nbsp;
                
</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>


编码实现>ASP.NET验证控件>验证日期输入

描述:输入xxxx/xx/xx样式的数字,才能验证通过,且符合时间规则。

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

<!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 id="Head1" runat="server">
    
<title>示范使用 CompareValidator 验证控件验证日期数据类型</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        请输入您所要查询的员工的出生日期:
        
<br />
        请输入下限:
        
<asp:TextBox ID="txtBirthdayLowerLimit" runat="server"></asp:TextBox>
        
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="" ControlToValidate="txtBirthdayLowerLimit"
            Display
="Dynamic" Operator="DataTypeCheck" SetFocusOnError="True" Type="Date">您输入的日期格式不正确!</asp:CompareValidator>
        
<br />
        请输入上限:
        
<asp:TextBox ID="txtBirthdayUpperLimit" runat="server"></asp:TextBox>
        
<asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="" ControlToValidate="txtBirthdayUpperLimit"
            Display
="Dynamic" Operator="DataTypeCheck" SetFocusOnError="True" Type="Date">您输入的日期格式不正确!</asp:CompareValidator>
        
<hr />
        
<asp:Button ID="btnSearch" runat="server" Text="开始查找" />
        
<hr />
        
<asp:GridView ID="LimingchStudio_GridView" runat="server" 
            AutoGenerateColumns
="False" BackColor="White" BorderColor="#336666" 
            BorderStyle
="Double" BorderWidth="3px" CellPadding="4" DataKeyNames="员工号码" 
            DataSourceID
="LimingchStudio_SqlDataSource" GridLines="Horizontal">
            
<FooterStyle BackColor="White" ForeColor="#333333" />
            
<RowStyle BackColor="White" ForeColor="#333333" />
            
<Columns>
                
<asp:BoundField DataField="员工号码" HeaderText="员工号码" InsertVisible="False" 
                    ReadOnly
="True" SortExpression="员工号码" />
                
<asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
                
<asp:BoundField DataField="性别" HeaderText="性别" SortExpression="性别" />
                
<asp:BoundField DataField="出生日期" DataFormatString="{0:d}" HeaderText="出生日期" 
                    SortExpression
="出生日期" />
                
<asp:BoundField DataField="当前薪资" HeaderText="当前薪资" SortExpression="当前薪资" />
                
<asp:BoundField DataField="部门" HeaderText="部门" SortExpression="部门" />
            
</Columns>
            
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        
</asp:GridView>
        
<

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[asp.net core]project.json(2)发布时间:2022-07-10
下一篇:
ASP.NET MVC 开源项目Kigg解读(2)——Kigg.Core第一部分发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap