Using the ValidationSummary control with ASP.NET MasterPages
Posted by: Malcolm Sheridan ,
on 7/16/2009,
in
Category ASP.NET
Abstract: The following article demonstrates how to create one validation summary control on a master page and have that display all your validation errors.
Using the ValidationSummary control with ASP.NET MasterPages
When you’re building web applications, it is important to validate the data before sending it to the database. If validation fails, a typical thing to do is display any validation errors to the user. This is normally done using the ValidationSummary control. This article will demonstrate how to use a central ValidationSummary control that resides on a master page to display all validation errors.
The key to getting this to work is to be able to set the ValidationGroup for the ValidationSummary from your web content pages. The ValidationGroup gets or sets the group of controls for which the ValidationSummary control displays validation messages. This needs to be changed because most ASP.NET validators have different validation groups.
To begin with open Visual Studio 2008 and choose File > New > Web > ASP.NET Web Application. Add a master page to the project and name it Site.Master. Add a ValidationSummary control to the page:
<asp:ValidationSummary ID="valErrorSummary" runat="server" Font-Size="Large" />
To expose this property to your web content pages, you’ll need to create a public property which sets the ValidationGroup property. This will be accessible from the web content pages via the MasterType directive. Add the following code to the master page’s code behind file:
C#
public string ValidationGroup
{
set { valErrorSummary.ValidationGroup = value; }
}
VB.NET
Public WriteOnly Property ValidationGroup() As String
Set(ByVal value As String)
valErrorSummary.ValidationGroup = value
End Set
End Property
That is basically all you need to do to the master page. The next step is to add a web content page to your project. Accept the default name provided by studio. To be able to call the ValidationGroup property from the web content page, you’ll need to add a new MasterType directive to the web content page:
<%@ MasterType VirtualPath="~/Site.Master" %>
This provides a way to create a strongly typed reference to the master page when the master page is accessed from the Master()()() property. Open the web content page and add the following code to the HTML mark-up:
Surname <asp:TextBox ID="txtSurname" runat="server" ValidationGroup="Surname"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Surname is required" ControlToValidate="txtSurname"
Display="None" ValidationGroup="Surname"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Save Data"
ValidationGroup="Surname" />
In the code above there is one TextBox, RequiredFieldValidator and a Button. The RequiredFieldValidator has its Display set to None. This is because if validation fails, we don’t want to display the error message beside the RequiredFieldvalidator, we want it displayed in the master pages ValidationSummary. All three controls have their ValidationGroup property set to Surname. If you ran this code now and entered nothing in the TextBox, you would see nothing telling you to input a surname. Why? Well that is because the you need to set the ValidationSummary’s ValidationGroup to Surname. You have access to the property through the MasterType directive, so add the following code to the page load event:
C#
protected void Page_Load(object sender, EventArgs e)
{
Master.ValidationGroup = "Surname";
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Master.ValidationGroup = "Surname"
End Sub
If you ran the project now and fail to enter a surname, you’ll see the error message displayed in the ValidationSummary.
By creating your validation controls this way, you’re eliminating the need to have ValidationSummary controls on each page. This makes administering your project much easier if you need to change any of them. Master pages are a great way to store central resources.
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @
malcolmsheridan