Ngôn ngữ ASP.NET - ValidationSummary
Điều khiển ValidationSummary
Điều khiển này được dùng để hiển thị ra bảng lỗi - tất cả các lỗi hiện có trên trang Web. Nếu điều khiển nào có dữ liệu không hợp lệ, chuỗi thông báo lỗi - giá trị thuộc tính ErrorMessage của Validation Control sẽ được hiển thị. Nếu giá trị của thuộc tính ErrorMessage không được xác định, thông báo lỗi đó sẽ không được xuất hiện trong bảng lỗi.
Các thuộc tính:
- HeaderText: Dòng tiêu đề của thông báo lỗi
- ShowMessageBox: Qui định bảng thông báo lỗi có được phép hiển thị như cửa sổ MessageBox hay không. Giá trị mặc định của thuộc tính này là False - không hiển thị.
- ShowSummary: Qui định bảng thông báo lỗi có được phép hiển thị hay không. Giá trị mặc định của thuộc tính này là True - được phép hiển thị.
- Xem thêm thuộc tính ValidationSummary
Ví dụ: Tạo trang web ValidationSummary.aspx, cho phép user nhập thông tin đăng ký khách hàng.
- Giao diện minh họa như hình dưới:
Khai báo các điều khiển:
Điều khiển | Kiểu | Thuộc tính | Giá trị |
rfvTenDN | RequiredField | ControlToValidate ErrorMessage |
txtTenDn Tên đăng nhập không được rỗng |
rfvMatKhau | RequiredField | ControlToValidate ErrorMessage |
txtMatKhau Mật khẩu không được rỗng |
rfvMauKhauNL | RequiredField | ControlToValidate ErrorMessage |
txtMauKhauNL Không được rỗng |
cvMatKhauNL | Compare | ControlToValidate ControlToCompare ErrorMessage |
txtMatKhauNL txtMatKhau Mật khẩu gõ lại không trùng với mật khẩu ban đầu |
rfvTenKH | RequiredField | ControlToValidate ErrorMessage |
txtTenKH Họ tên không được rỗng |
cvNgaySinh | Compare | ControlToValidate Operator ErrorMessage |
txtNgaySinh DataTypeCheck Ngày không hợp lệ |
revEmail | RegularExpression | ControlToValidate ValidationExpression ErrorMessage |
txtEmail Email không hợp lệ |
rvThuNhap | Range | ControlToValidate MaximumValue MinimumValue Type ErrorMessage |
txtThuNhap 50000000 1000000 Integer Thu nhập từ 1 – 50 triệu |
vsDanhSachLoi | ValidationSummary | HeaderText ShowMessageBox |
Danh sách các lỗi true |
Code trang ValidationSummary.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dangkythanhvien.aspx.cs" Inherits="dangkythanhvien" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 600px;
border: 2px solid #00FFFF;
}
.auto-style2 {
text-align: center;
}
.auto-style3 {
width: 115px;
}
.auto-style4 {
background-color: #66CCFF;
}
.auto-style5 {
width: 115px;
height: 128px;
}
.auto-style6 {
height: 128px;
width: 316px;
}
.auto-style7 {
width: 115px;
height: 26px;
}
.auto-style8 {
height: 26px;
width: 316px;
}
.auto-style9 {
width: 316px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" class="auto-style1">
<tr>
<td class="auto-style2" colspan="2" style="background-color: #CCCCCC">Đăng ký thành viên</td>
</tr>
<tr>
<td colspan="2" style="background-color: #00FFCC">Thông tin đăng nhập</td>
</tr>
<tr>
<td class="auto-style3">Tên đăng nhập:</td>
<td class="auto-style9">
<asp:TextBox ID="txtTenDn" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTenDN" runat="server" ControlToValidate="txtTenDn" ErrorMessage="Tên đăng nhập không được rỗng" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style3">Mật khẩu:</td>
<td class="auto-style9">
<asp:TextBox ID="txtMatkhau" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvMatKhau" runat="server" ControlToValidate="txtMatkhau" ErrorMessage="Mật khẩu không được rỗng" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style3">Nhập lại mật khẩu:</td>
<td class="auto-style9">
<asp:TextBox ID="txtMatKhauNL" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvMatKhauNL" runat="server" ControlToValidate="txtMatKhauNL" Display="Dynamic" ErrorMessage="Mật khẩu nhập lại không được rỗng" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvMatKhauNL" runat="server" ControlToCompare="txtMatKhau" ControlToValidate="txtMatKhauNL" ErrorMessage="Mật khẩu gõ lại không trùng với mật khẩu ban đầu" ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style4" colspan="2">Thông tin cá nhân</td>
</tr>
<tr>
<td class="auto-style3">Họ tên</td>
<td class="auto-style9">
<asp:TextBox ID="txtHoten" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvHoten" runat="server" ControlToValidate="txtHoten" ErrorMessage="Họ tên không được rỗng" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style3">Ngày sinh</td>
<td class="auto-style9">
<asp:TextBox ID="txtNgaysinh" runat="server"></asp:TextBox>
<asp:CompareValidator ID="cvNgaysinh" runat="server" ControlToValidate="txtNgaysinh" ErrorMessage="Ngày không hợp lệ" ForeColor="Red" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style3">Giới tính</td>
<td class="auto-style9">
<asp:RadioButton ID="rdbNam" runat="server" Checked="True" Text="Nam" />
<asp:RadioButton ID="rdbNu" runat="server" Text="Nữ" />
</td>
</tr>
<tr>
<td class="auto-style3">Email</td>
<td class="auto-style9">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="Email không hợp lệ" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style7">Thu nhập</td>
<td class="auto-style8">
<asp:TextBox ID="txtThuNhap" runat="server"></asp:TextBox>
<asp:RangeValidator ID="rvThuNhap" runat="server" ControlToValidate="txtThuNhap" ErrorMessage="Thu nhập 1-50tr" ForeColor="Red" MaximumValue="50000000" MinimumValue="1000000" Type="Integer"></asp:RangeValidator>
</td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td class="auto-style9">
<asp:Button ID="btnDangki" runat="server" Text="Đăng ký" Width="73px" />
</td>
</tr>
<tr>
<td class="auto-style5"></td>
<td class="auto-style6">
<asp:ValidationSummary ID="vsDanhSachLoi" runat="server" ForeColor="Red" HeaderText="Danh sách các lỗi" ShowMessageBox="True" DisplayMode="List" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Thực thi trang web ValidationSummary.aspx:
Sau khi nhập các thông tin: thông tin họ tên để rỗng, mật khẩu nhập lại không trùng với mật khẩu ban đầu. Click vào nút đăng ký cho kết quả: