Wednesday, 28 July 2010

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.
Error_Message

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

No comments:

Post a Comment