ASP.Net ValidationSummary Example, How to <asp:ValidationSummary> Control allows you to summarize the error messages in asp.net c#

<asp:ValidationSummary
id="programmaticID"
DisplayMode="BulletList | List | SingleParagraph"
EnableClientScript="true | false"
ShowSummary="true | false"
ShowMessageBox="true | false"
HeaderText="TextToDisplayAsSummaryTitle"
runat="server"/>

The ValidationSummary control allows you to summarize the error messages from all validation controls on a Web page in a single location. The summary can be displayed as a list, a bulleted list, or a single paragraph, based on the value of the DisplayMode property. The error message displayed in the ValidationSummary control for each validation control on the page is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed in the ValidationSummary control for that validation control. You can also specify a custom title in the heading section of the ValidationSummary control by setting the HeaderText property.

You can control whether the ValidationSummary control is displayed or hidden by setting the ShowSummary property. The summary can also be displayed in a message box by setting the ShowMessageBox property to true.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected void ButtonExample_Click(object sender, System.EventArgs e) {
lbl1Example.Text = "Your Country: " +
txt1Example.Text.ToString() +
"<br />Region: " +
txt2Example.Text.ToString() +
"<br />City: " +
txt3Example.Text.ToString();

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.Net ValidationSummary Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lbl1Example" runat="server" Font-Size="Large" ForeColor="BlueViolet"></asp:Label>
<br /><br />
<asp:Label ID="lbl2Example" runat="server" Text="Country Name" AssociatedControlID="txt1Example"></asp:Label>
<asp:TextBox ID="txt1Example" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorExample"
runat="server"
ControlToValidate="txt1Example"
ErrorMessage='Input Country!'
EnableClientScript="true"
SetFocusOnError="true"
Text="*"
>
</asp:RequiredFieldValidator>
<br />

<asp:Label ID="lbl3Example" runat="server" Text="Region Name" AssociatedControlID="txt2Example"></asp:Label>
<asp:TextBox ID="txt2Example" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2"
runat="server"
ControlToValidate="txt2Example"
ErrorMessage='Input Region!'
EnableClientScript="true"
SetFocusOnError="true"
Text="*"
>
</asp:RequiredFieldValidator>
<br />

<asp:Label ID="lbl4Example" runat="server" Text="City Name" AssociatedControlID="txt3Example"></asp:Label>
<asp:TextBox ID="txt3Example" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator3"
runat="server"
ControlToValidate="txt3Example"
ErrorMessage='Input Region!'
EnableClientScript="true"
SetFocusOnError="true"
Text="*"
>
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="ButtonExample" runat="server" Text="Submit" OnClick="ButtonExample_Click" />
<br /><br />
<asp:ValidationSummary ID="ValidationSummaryExample" runat="server" HeaderText="Following error occurs:" ShowMessageBox="false" DisplayMode="BulletList" ShowSummary="true" />
</div>
</form>
</body>
</html>
Tags: ,
Hot on Web:


About author