What is the ValidationSummary?The validation summary is an ASP.NET control that allows you to easily display validation error messages. It works by taking all of the validation error messages from an invalid page and displays them in a single location. The validation summary is used on pages that contain validators to simplify and organize error messages for the user.
Using the ValidationSummaryTo demonstrate the use of a validation summary, we will need to create a simple web page with a few validators on it. To do this, create a new ASP.NET Empty Web Site and:
Code---
Using the ValidationSummaryTo demonstrate the use of a validation summary, we will need to create a simple web page with a few validators on it. To do this, create a new ASP.NET Empty Web Site and:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a textbox onto the web form.
- Drag and drop a requiredfieldvalidator onto the web form.
- Change the ErrorMessage property to 'TextBox1 is required.'
- Set the ControlToValidate property to 'TextBox1'.
- Set the Text property to '*'.
- Add a break line.
- Drag and drop a textbox onto the web form.
- Drag and drop a requiredfieldvalidator onto the web form.
- Change the ErrorMessage property to 'TextBox2 is required.'
- Set the ControlToValidate property to 'TextBox2'.
- Set the Text property to '*'.
- Add a break line.
- Drag and drop a button onto the web form.
- Add a break line.
- Drag and drop a validationsummary onto the web form.
Code---
<%@ 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 Button1_Click(object sender, System.EventArgs e) {
Label1.Text = "Your Country: " +
TextBox1.Text.ToString() +
"<br />Region: " +
TextBox2.Text.ToString() +
"<br />City: " +
TextBox3.Text.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ValidationSummary example: how to use ValidationSummary control in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="BlueViolet"></asp:Label>
<br /><br />
<asp:Label ID="Label2" runat="server" Text="Country Name" AssociatedControlID="TextBox1"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="TextBox1"
ErrorMessage='Input Country!'
EnableClientScript="true"
SetFocusOnError="true"
Text="*"
>
</asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label3" runat="server" Text="Region Name" AssociatedControlID="TextBox2"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2"
runat="server"
ControlToValidate="TextBox2"
ErrorMessage='Input Region!'
EnableClientScript="true"
SetFocusOnError="true"
Text="*"
>
</asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label4" runat="server" Text="City Name" AssociatedControlID="TextBox3"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator3"
runat="server"
ControlToValidate="TextBox3"
ErrorMessage='Input Region!'
EnableClientScript="true"
SetFocusOnError="true"
Text="*"
>
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<br /><br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Following error occurs:" ShowMessageBox="false" DisplayMode="BulletList" ShowSummary="true" />
</div>
</form>
</body>
</html>
No comments:
Post a Comment