Meditation and Mindfulness for Software / IT Professionals. Conducted by Bipin Joshi in Thane. Read more...

Using Validation Groups Inside ASP.NET User Controls

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 let's create a simple user control. Have a look at the figure that shows such a user control:

The Web Form shown in  the above figure houses two instances of a user control - TestUC.ascx. The user control consists of a TextBox, a RequiredFieldValidator and a Button. The markup of the user control is shown below:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
ControlToValidate="TextBox1" ErrorMessage="*" Font-Bold="True" 
Font-Size="30px" ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>

The Web Form contains two instances of TestUC.ascx as shown below:

<%@ Page Language="C#" ... %>
<%@ Register Src="~/TestUC.ascx" TagPrefix="uc1" TagName="TestUC" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <uc1:TestUC runat="server" id="TestUC1" />
        <br />
        <uc1:TestUC runat="server" id="TestUC2" />
    </form>
</body>
</html>

If you set the ValidationGroup property of TextBox1, RequiredFieldValidator1 and Button1 to some string (say VG) and then run the Web Form you will find that upon clicking any of the Submit buttons both the RequiredFieldValidatior controls show the error.

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
  • Generate a unique value for ValidationGroup inside the user control

The first solution requires you to create a property (say ValidationGroup) in the user control. This property will accept a unique string value for ValidationGroup and will assign it to all the required server controls. The following code shows how this property can be created:

public string ValidationGroup
{
  get
  {
    return Button1.ValidationGroup;
  }
  set
  {
    Button1.ValidationGroup = value;
    TextBox1.ValidationGroup = value;
    RequiredFieldValidator1.ValidationGroup = value;
  }
}

As you can see, the set block of ValidationGroup property (you can name this property anything you want) assigns the supplied value to the ValidationGroup of Button1, TextBox1 and RequiredFieldValidator1. Once the ValidationGroup property is created you can set it from the Web Form as follows:

 <form id="form1" runat="server">
  <uc1:TestUC runat="server" id="TestUC1" ValidationGroup="Group1" />
  <br />
  <uc1: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 outlined 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)
{
  if(!IsPostBack)
  { 
    string vgId = Guid.NewGuid().ToString();
    Button1.ValidationGroup = vgId;
    TextBox1.ValidationGroup = vgId;
    RequiredFieldValidator1.ValidationGroup = vgId;
  }
}

The above code shows the Page_Load event handler of the user control. The Page_Load event handler 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 Web Form will work as expected.

 




Bipin Joshi is a software consultant, an author and a yoga mentor having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 29 Oct 2013



Tags : ASP.NET Web Forms Server Controls