What does Validation groups do? Well, they permit you to validate data entry controls in the groups. Server controls like validation controls, Button and TextBox have ValidationGroup attributes that have a string value.
All the server controls which have the same ValidationGroup value acts as a single validation group. Validation groups are necessary when you want to analyze only a small set of controls from several controls housed on a Web Form. Using validation groups is very easy and straight. But if you have a validation group inside a user control and there are many control instances on a Web Form you come across some problems.
To understand in details let’s develop a simple user control.
The Web Form has two instances of a user control – TestUC.ascx. The user control has a TextBox, a RequiredFieldValidator and a Button. The codes of the user control is shared below:
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server”
ControlToValidate=”TextBox1″ ErrorMessage=”*” Font-Bold=”True”
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Submit” />
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True” ForeColor=”Red”></asp:Label>
The Web Form has two instances of TestUC.ascx as mentioned below:
<%@ Page Language=”C#” … %>
<%@ Register Src=”~/TestUC.ascx” TagPrefix=”uc1″ TagName=”TestUC” %>
<form id=”form1″ runat=”server”>
<uc1:TestUC runat=”server” id=”TestUC1″ />
<uc1:TestUC runat=”server” id=”TestUC2″ />
If you put the ValidationGroup property of TextBox1, RequiredFieldValidator1 and Button1 to some string (e.g. VG) and then carry out the Web Form you will find that once getting clicked either of the Submit buttons, both the RequiredFieldValidatior controls display an error.
This behavior though not expected but occurs as both the instances of the user control using same ValidationGroup value.
To remove this problem you can take two approaches:
Provide a different and unique ValidationGroup value to the user control from the Web Form
Produce a unique value for ValidationGroup inside the user control
The first solution needs creation of a property (e.g. ValidationGroup) in user control. This attribute will accept a unique string value for the ValidationGroup and will assign to all the other necessary server controls. This is how the property can be developed:
public string ValidationGroup
Button1.ValidationGroup = value;
TextBox1.ValidationGroup = value;
RequiredFieldValidator1.ValidationGroup = value;
As shown, the set block of ValidationGroup attribute assign the given value to the ValidationGroup of Button1, TextBox1 and RequiredFieldValidator1.Once the ValidationGroup property is developed you can set it from the Web Form as given below:
<form id=”form1″ runat=”server”>
<uc1:TestUC runat=”server” id=”TestUC1″ ValidationGroup=”Group1″ />
<uc1:TestUC runat=”server” id=”TestUC2″ ValidationGroup=”Group2″ />
The ValidationGroup property of TestUC1 and TestUC2 is set to different and unique values. Now each user controls will work as per expectations.
In the next approach mentioned above you can auto-generate ValidationGroup value within the user control itself. The following code tells you how:
protected void Page_Load(object sender, EventArgs e)
string vgId = Guid.NewGuid().ToString();
Button1.ValidationGroup = vgId;
TextBox1.ValidationGroup = vgId;
RequiredFieldValidator1.ValidationGroup = vgId;
The above code reveals the Page_Load event handler of the user control. The Page_Load event handler develops a new Guid using Guid.NewGuid() method. Next it sets ValidationGroup property of Button1, TextBox1 and RequiredFieldValidaror1 to this Guid string. Thus, every example of user control gets a unique ValidationGroup value and the Web Form woks as expected.
That’s it for now! Keep coding!!
If you want to enhance yourself in Dot Net Course and improve yourself through Dot NET training program; our institute would be of great help and support. We offer well structured program for the Best Dot Net Course.
Stay connected to CRB Tech for your technical up-gradation and to remain updated with all the happenings in the world of Dot Net.