Validation Groups
Validation groups allow you to validate data entry controls in groups. Server
controls such as validation controls, Button and TextBox have
ValidationGroup property that takes a string value. All the server controls
having the same ValidationGroup value act as one validation group
Validation groups come handy in situations where you wish to validate only
a small set of controls from many controls housed on a Web Form. Using
validation groups is quite easy and straightforward. However, if you have a
validation group inside a user control and there are more than one user
control instances on a Web Form you face some problem.
To understand the problem and possible solutions lete's create a simple user
control. Have a look at the figure that shows such a user control:
114
localhost 1245/W x X
Clocalhost 1245N
Submit
Submit
The Web Form shown in
the above figure houses two
control TestUC.ascx. The user
- instances of a user
control consists of a TextBox,a
RequiredFieldValidatorand a Button. The
markup of the user control is
shown below:
FleEdit Vie t
Projed uld Debug Test Analyze Tools tensionswindo earchP
mas.pe9
DEDug Any CPU
IS Express (Googie Chrome)D Lve Share
eShee Ste.ae WebFornm1.aspx* Solution Eplore
le Language- CE asterPageFile="-/Site.Master" AutoEventiireup*rue CodeBehind="debf
asELotent
Dcortenz ConEentPLaceko LderID- "Haincontent" runat-"server
tBxi runat"server"ajasp:TextRar Search Soiution Explorer C P
p:Required ieldatidator D=*Requiredfieldvalidator1 runat="server
ControtTovalidate="TextSo:1 ErTorMessage=**" Fant-Bold="TIue LSolution'masterpage ot
Font-Sze="i3p ForetoLor=*Red /aspRequireoeletaiacata masterpage
ID="Putton1 runat="server OnClick"Buttonclick Tert="Subnit" Connected services
Properties
5 L a b e t ID=*iabeli" runat="server Font-3old="Tue ForeColo ISp:Label» D References
asp:Lonteat App Data
App Start
Content
tonts
Scripts
Aboutaspx
Bundle.conig
Contactaspx
Dafaultaspx
favicon.ico
Global.asax
packages.config
Ln 13 Ch 1 SPC CRLT Ste.Master
2% Noisues iaus
Desigrs SplrSource
Add to Source Control
Select Repository
2Ready
instances of TestUC.ascx as shown below
The Web Form contains two
Gt Proect Buld Debug Test Analyze Tools Extensons Windov
SearchP mas- page
DeugAnyCU SEpreis (Googie Chrome) D Lve Share
testuCasar *eheeticm SitaMarter WebForm1aspr Soluton Erplorer
age Langaage=*C
Begaxte re-Testu.ase Tagrefi*ucl Tagtane"Testuc
aanns e.ol.org/189R/ttnls Search Soiutior FvpinreriCt
Solution masterpage (1 ol
masterpage
Connected Services
natsorver Properies
serveriTestu Ë Reterences
e unate"server ia"Tertuca A p p Dota
App Sta
Content
fonts
Scripts
e About.aspx
Bundle.contig
Contacaspx
Defaultaspx
Efavicon.ico
Global.asax
packages.config
L 11 h 15 SPL CRL Site.Master
soliSourre
Addto Source Control Select Repository
fyou set the ValidationGroup property of TextBox1,
RequiredField Validator1 and Button1 to some string (say VG) and then run
the Web Fom you will find that upon clicking any of the Submit buttons
both the RequiredField Validatior controls show the-error.
X
localinost 1245Wex
C localhost:1245/ E
Submit
Submit
This behavior is undesirable and causes due to the fact that both the
instances of the user control use the same ValidationGroup value.
To overcome this problem you can take two approaches:
Supply a different and unique ValidationGroup value to the user
control from the Web Form
116
inside the user contro
Generate a unique value for ValidationGroup
ValidationGroup) in
to create a property (say
he firstsolution requires you will value for
the user control. This property accept a unique string Ine
server controls.
ValidationGroup and will assign it to all the required
be created:
following code shows how this property can
public string ValidationGroup
Get
return Button1.ValidationGroup;
set
{
Button1.ValidationGroup = value;
TextBox1.ValidationGroup = value
RequiredFieldValidator1.ValidationGroup=value
can name
As youcan-see, thesetblock of ValidationGroup property (you
this property.anything you want) assigns thesupplied value to the
Once
ValidationGroup of Button1, TextBox1and RequiredFieldValidator1. Form
the ValidationGroup property is created you can set it from the Web
as follows
sform id="form1" runat="server
<uc1:TestUC runat="server" id="TestUC1" ValidationGroup="Group1"
<br >
suc1:TestUC runat-"server" id="TestUC2" ValidationGroup="Group2" >
<form>
As you can see the ValidationGroup property of TestUC1 and TestUC2 is
set to some different and unique value. Now the individual user controls will
Work as expected.
In the second approach outined above you can auto-generate
ValidationGroup value inside the user control itself. The following code
shows how:
protected void Page_Load(object sender, EventArgs e)
117
if(lsPostBack)
string vgld Guid.NewGuid().ToString()
Button1.ValidationGroup = vgld;
TextBox1.ValidationGroup = vgld;
RequiredFieldValidator1.ValidationGroup = vgld;
The above code shows the PageLoad event handler of the user control.
The Page_Load event handier creates a new Guid using Guid.NewGuid()
method. It then sets the ValidationGroup property of Button1, TextBox1 and
RequiredFieldValidaror1 to this Guid string. This way, every instance of
user control will get a unique ValidationGroup value and the VWeb Form will
work as expected.