ASP.NET Tutorial/Validation/ValidationSummary
Содержание
Important properties of ValidationSummary control
DisplayMode: how the error messages are formatted.
Possible values are BulletList, List, and SingleParagraph.
HeaderText: display header text above the validation summary.
ShowMessageBox: display a popup alert box.
ShowSummary: hide the validation summary in the page.
<%@ Page Language="C#" %>
<!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>Show Summary Popup</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary
id="ValidationSummary1"
ShowMessageBox="true"
ShowSummary="false"
Runat="server" />
<asp:Label
id="lblFirstName"
Text="First Name:"
AssociatedControlID="txtFirstName"
Runat="server" />
<br />
<asp:TextBox
id="txtFirstName"
Runat="server" />
<asp:RequiredFieldValidator
id="reqFirstName"
ErrorMessage="First Name is required"
ControlToValidate="txtFirstName"
Display="None"
Runat="server" />
<br /><br />
<asp:Label
id="lblLastName"
Text="Last Name:"
AssociatedControlID="txtLastName"
Runat="server" />
<br />
<asp:TextBox
id="txtLastName"
Runat="server" />
<asp:RequiredFieldValidator
id="reqLastName"
ErrorMessage="Last Name is required"
ControlToValidate="txtLastName"
Display="None"
Runat="server" />
<br /><br />
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" />
</div>
</form>
</body>
</html>
RequiredFieldValidatorSummary DisplayMode
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Required Field Validation</title>
</head>
<body>
<form runat="server" ID="frmBugs">
<asp:Label ID="lblMsg"
Text="Please report your bug here"
ForeColor="red" Font-Name="Verdana"
Font-Size="10" runat=server />
<ASP:DropDownList id=ddlBooks runat=server>
<asp:ListItem>-- Please Pick A Book --</asp:ListItem>
<asp:ListItem>Programming ASP.NET</asp:ListItem>
<asp:ListItem>Programming .NET Windows Applications</asp:ListItem>
<asp:ListItem>Programming C#</asp:ListItem>
<asp:ListItem>Programming Visual Basic 2005</asp:ListItem>
<asp:ListItem>
Teach Yourself C++ In 21 Days
</asp:ListItem>
<asp:ListItem>
Teach Yourself C++ In 24 Hours
</asp:ListItem>
<asp:ListItem>TY C++ In 10 Minutes</asp:ListItem>
<asp:ListItem>TY More C++ In 21 Days</asp:ListItem>
<asp:ListItem>C++ Unleashed</asp:ListItem>
</ASP:DropDownList>
</td>
<!-- Validator for the drop down -->
<td align=center rowspan=1>
<asp:RequiredFieldValidator
id="reqFieldBooks"
ControlToValidate="ddlBooks"
Display="Static"
InitialValue="-- Please Pick A Book --"
ErrorMessage = "You did not choose a book from the drop-down"
Width="100%" runat=server> * </asp:RequiredFieldValidator>
<td>
<ASP:RadioButtonList id=rblEdition
RepeatLayout="Flow" runat=server>
<asp:ListItem>1st</asp:ListItem>
<asp:ListItem>2nd</asp:ListItem>
<asp:ListItem>3rd</asp:ListItem>
<asp:ListItem>4th</asp:ListItem>
</ASP:RadioButtonList>
</td>
<td align=center rowspan=1>
<asp:RequiredFieldValidator
id="reqFieldEdition"
ControlToValidate="rblEdition"
ErrorMessage = "You did not pick an edition"
Display="Static"
InitialValue=""
Width="100%" runat=server>*</asp:RequiredFieldValidator>
<ASP:TextBox id=txtBug
runat=server
width="183px"
textmode="MultiLine"
height="68px"/>
<asp:RequiredFieldValidator
id="reqFieldBug"
ControlToValidate="txtBug"
ErrorMessage = "You must provide bug details"
Display="Static"
Width="100%" runat=server>*</asp:RequiredFieldValidator>
<asp:DropDownList id="lstDisplay"
AutoPostBack=true
OnSelectedIndexChanged="lstDisplay_SelectedIndexChanged"
runat=server>
<asp:ListItem Selected ="true">Summary</asp:ListItem>
<asp:ListItem>Msg. Box</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="lstFormat"
AutoPostBack=true
OnSelectedIndexChanged="lstFormat_SelectedIndexChanged"
runat=server>
<asp:ListItem>List</asp:ListItem>
<asp:ListItem Selected="true">Bulleted List</asp:ListItem>
<asp:ListItem>Single Paragraph</asp:ListItem>
</asp:DropDownList>
<ASP:Button id=btnSubmit
text="Submit Bug" runat=server OnClick="btnSubmit_Click" />
<asp:ValidationSummary
ID="ValSum" runat="server"
DisplayMode="BulletList"
HeaderText="The following errors were found: " ShowSummary="True" />
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default_aspx : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblMsg.Text = "Page is Valid!";
}
else
{
lblMsg.Text = "Some of the required fields are empty";
}
}
protected void lstFormat_SelectedIndexChanged(object sender, EventArgs e)
{
ValSum.DisplayMode =
(ValidationSummaryDisplayMode)
lstFormat.SelectedIndex;
}
protected void lstDisplay_SelectedIndexChanged(object sender, EventArgs e)
{
ValSum.ShowSummary = lstDisplay.SelectedIndex == 0;
ValSum.ShowMessageBox = lstDisplay.SelectedIndex == 1;
}
}
Use ValidationSummary to display message box (C#)
File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ValidationSummary" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Validation Summary</title>
</head>
<body>
<form id="form1" runat="server">
<div>
A number (1 to 10):
<asp:textbox id="txtValidated" runat="server"></asp:textbox>
<asp:rangevalidator id="RangeValidator"
runat="server"
Type="Integer"
MinimumValue="1"
MaximumValue="10"
ControlToValidate="txtValidated"
Text="<img src="ErrorIcon.jpg" alt="Error" />"
ErrorMessage="The First Number Is Not In The Range">
</asp:rangevalidator>
Not validated:
<asp:textbox id="txtNotValidated" runat="server"></asp:textbox><br/>
<br />
<asp:button id="cmdOK"
runat="server"
Text="OK"
OnClick="cmdOK_Click"
Width="44px"></asp:button><br />
<br />
<asp:label id="lblMessage"
runat="server"
EnableViewState="False"></asp:label><br />
<br />
<asp:ValidationSummary id="ValidationSummary1"
runat="server"
ShowMessageBox="True"></asp:ValidationSummary>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ValidationSummary : System.Web.UI.Page
{
protected void cmdOK_Click(object sender, EventArgs e)
{
if (!Page.IsValid) return;
lblMessage.Text = "cmdOK_Click event handler executed.";
}
}
ValidationSummary DisplayMode=BulletList
<%@ Page Language="C#"%>
<!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>Using ValidationSummary</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Book
<br />
<asp:DropDownList ID="lstBooks" runat="server">
<asp:ListItem Value="0" Selected="True">Please pick a book</asp:ListItem>
<asp:ListItem Value="1">A</asp:ListItem>
<asp:ListItem Value="2">B</asp:ListItem>
<asp:ListItem Value="3">C</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqBook"
runat="server"
ControlToValidate="lstBooks"
SetFocusOnError="true"
Text="*"
InitialValue="0"
Display="static"
ErrorMessage="Please choose a book from list"/>
User Name:<br />
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqUserName"
runat="server"
ControlToValidate="txtUserName"
SetFocusOnError="true"
Text="*"
Display="static"
ErrorMessage="Please enter a User Name"/>
Password:<br />
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqPassword"
runat="server"
ControlToValidate="txtPassword"
SetFocusOnError="true"
Text="*"
Display="static"
ErrorMessage="Please enter a password"/>
<asp:ValidationSummary id="valSum"
runat="server"
DisplayMode="BulletList"
HeaderText="The following errors were found:"
ShowMessageBox="false"
ShowSummary="true" />
<asp:Button ID="btnSubmit" Text="Click this to test validation" runat="server" />
</div>
</form>
</body>
</html>
ValidationSummary displays a list of validation errors
<%@ Page Language="C#" %>
<!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>Show ValidationSummary</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary
id="ValidationSummary1"
Runat="server" />
<asp:Label
id="lblFirstName"
Text="First Name:"
AssociatedControlID="txtFirstName"
Runat="server" />
<br />
<asp:TextBox
id="txtFirstName"
Runat="server" />
<asp:RequiredFieldValidator
id="reqFirstName"
Text="(Required)"
ErrorMessage="First Name is required"
ControlToValidate="txtFirstName"
Runat="server" />
<br /><br />
<asp:Label
id="lblLastName"
Text="Last Name:"
AssociatedControlID="txtLastName"
Runat="server" />
<br />
<asp:TextBox
id="txtLastName"
Runat="server" />
<asp:RequiredFieldValidator
id="reqLastName"
Text="(Required)"
ErrorMessage="Last Name is required"
ControlToValidate="txtLastName"
Runat="server" />
<br /><br />
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" />
</div>
</form>
</body>
</html>