Click here to Skip to main content
12,302,514 members (63,004 online)
Click here to Skip to main content

Stats

25.5K views
707 downloads
30 bookmarked
Posted

Template Based Multiple Control Validator

, 25 Mar 2009 CPOL
A templated server control that allows us to validate multiple controls with the same validation controls
ValidationAggregator
SampleWebSite
App_Code
App_Data
Bin
ValidationAggregator.dll
ValidationAggregator.pdb
ValidationAggregator
bin
Properties
ValidationAggregator.4.0.resharper
ValidationAggregator.4.0.resharper.user
ValidationAggregator.gpState
ValidationAggregator.suo
bin
Release
ValidationAggregator.dll
Properties
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="ValidationAggregator" namespace="ValidationAggregator" tagprefix="cc1" %>

<!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></title>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    	<table>
			<tr>
				<td>
					Height :</td>
				<td>
					<asp:TextBox ID="txtHeight" runat="server" Width="93px"></asp:TextBox>
				</td>
			</tr>
			<tr>
				<td>
					Width :</td>
				<td>
					<asp:TextBox ID="txtWidth" runat="server" Width="93px"></asp:TextBox>
				</td>
			</tr>
			<tr>
				<td>
					Depth :</td>
				<td>
					<asp:TextBox ID="txtDepth" runat="server" Width="93px"></asp:TextBox>
				</td>
			</tr>
			<tr>
				<td>
				</td>
				<td>
				</td>
			</tr>
			<tr>
				<td>
					&nbsp;</td>
				<td>
					<asp:ValidationSummary ID="ValidationSummary1" runat="server" 
						ValidationGroup="InsertObject" />
				</td>
			</tr>
			<tr>
				<td>
					&nbsp;</td>
				<td>
					<asp:Button ID="btnInsert" runat="server" Text="Insert Object" 
						ValidationGroup="InsertObject" />
				</td>
			</tr>
		</table>
		<cc1:ValidationTemplate ID="ValidationTemplate1" runat="server" 
			DefaultValidationGroup="InsertObject">
			<Validators>
				<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
					ErrorMessage="{0} is mandatory" Display="Dynamic">*</asp:RequiredFieldValidator>
				<asp:RegularExpressionValidator  runat="server" 
					ErrorMessage="{0} is incorrect" Display="Dynamic" ValidationExpression="\d+">*</asp:RegularExpressionValidator>
				<asp:RangeValidator ID="RangeValidator1" runat="server" 
					ErrorMessage="{0} should be between 10 and 100" MaximumValue="100" 
					MinimumValue="10" Display="Dynamic" Type="Integer">*</asp:RangeValidator>
			</Validators>
			<ControlsToValidate>
				<cc1:ControlDesc CID="txtHeight" Label="Height" />
				<cc1:ControlDesc CID="txtWidth" Label="Width" />
				<cc1:ControlDesc CID="txtDepth" Label="Depth" />
			</ControlsToValidate>
		</cc1:ValidationTemplate>
    
    </div>
	<br />
	<br />
    <div>
		<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
			DataObjectTypeName="MyObject" SelectMethod="GetObjects" 
			TypeName="MyObjectsList" UpdateMethod="UpdateObject"></asp:ObjectDataSource>
		<table>
			<tr>
				<td>
		<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
			DataKeyNames="ID" DataSourceID="ObjectDataSource1">
			<Columns>
				<asp:CommandField ShowEditButton="True" ValidationGroup="UpdateObject" />
				<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ReadOnly="True" />
				<asp:TemplateField HeaderText="Height" SortExpression="Height">
					<EditItemTemplate>
						<asp:TextBox ID="txtHeight" runat="server" Text='<%# Bind("Height") %>'  Width="40px"></asp:TextBox>
						<cc1:Validator ID="Validator1" runat="server" ControlToValidate="txtHeight" 
							Label="Height" TemplateId="ValidationTemplate1" 
							ValidationGroup="UpdateObject" />
					</EditItemTemplate>
					<ItemTemplate>
						<asp:Label ID="Label1" runat="server" Text='<%# Bind("Height") %>'></asp:Label>
					</ItemTemplate>
				</asp:TemplateField>
				<asp:TemplateField HeaderText="Width" SortExpression="Width">
					<EditItemTemplate>
						<asp:TextBox ID="txtWidth" runat="server" Text='<%# Bind("Width") %>'  Width="40px"></asp:TextBox>
						<cc1:Validator ID="Validator2" runat="server" ControlToValidate="txtWidth" 
							Label="Width" TemplateId="ValidationTemplate1" ValidationGroup="UpdateObject" />
					</EditItemTemplate>
					<ItemTemplate>
						<asp:Label ID="Label2" runat="server" Text='<%# Bind("Width") %>'></asp:Label>
					</ItemTemplate>
				</asp:TemplateField>
				<asp:TemplateField HeaderText="Depth" SortExpression="Depth">
					<EditItemTemplate>
						<asp:TextBox ID="txtDepth" runat="server" Text='<%# Bind("Depth") %>'  Width="40px"></asp:TextBox>
						<cc1:Validator ID="Validator3" runat="server" ControlToValidate="txtDepth" 
							Label="Depth" TemplateId="ValidationTemplate1" ValidationGroup="UpdateObject" />
					</EditItemTemplate>
					<ItemTemplate>
						<asp:Label ID="Label3" runat="server" Text='<%# Bind("Depth") %>'></asp:Label>
					</ItemTemplate>
				</asp:TemplateField>
			</Columns>
		</asp:GridView>
				</td>
				<td>
					<asp:ValidationSummary ID="ValidationSummary2" runat="server" 
						ValidationGroup="UpdateObject" />
				</td>
			</tr>
		</table>
		<br />
	</div>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hisham Kedjour
Software Developer (Senior)
Algeria Algeria
Fascinated by good architectures, well thought classes and clean code. I work as a software developer since 2004.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 25 Mar 2009
Article Copyright 2009 by Hisham Kedjour
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid