Recently I had this requirement where in a single button click I need to call multiple Validation groups. The scenario is something like this. I had a ASP.NET page where there were 5 user controls and each had their own validation controls embedded in them. Each user control had its own validation controls and they had their own Validation group defined. All these user controls were placed in a single page which only had two button “Submit” and “Canel”. On click of the “Submit” button all the validation controls in the form should execute. The problem here is that there are five different validation groups in the same page. To top it all you can only assign one validation group to a button’ ValidationGroup property. If you don’t assign any validation group then on click of the submit button the validation controls which don’t have any validation group defined for them will only be fired. Validation controls which have validation group property defined will not get fired. This is the default behavior of validation controls. So how to call the validation controls of different groups? The answer lies in calling the “Page_ClientValidate” javascript method with the validation group name.
Lets try to understand how this all words with some code.The HTML code of the page looks something like this.
1: <table width="100%">
2: <tr>
3: <td>
4: <asp:TextBox ID="TextBox1"
5: runat="server"></asp:TextBox>
6: <asp:RequiredFieldValidator
7: ID="RequiredFieldValidator1" runat="server"
8: ErrorMessage="RequiredFieldValidator"
9: ValidationGroup="Group1" ControlToValidate="TextBox1">
10: </asp:RequiredFieldValidator>
11: </td>
12: </tr>
13: <tr>
14: <td>
15: <asp:TextBox ID="TextBox2"
16: runat="server"></asp:TextBox>
17: <asp:RequiredFieldValidator
18: ID="RequiredFieldValidator2" runat="server"
19: ErrorMessage="RequiredFieldValidator"
20: ValidationGroup="Group2" ControlToValidate="TextBox2">
21: </asp:RequiredFieldValidator>
22: </td>
23: </tr>
24: <tr>
25: <td>
26: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator"
27: ValidationGroup="Group3" ControlToValidate="TextBox3">
28: </asp:RequiredFieldValidator>
29: </td>
30: </tr>
31: <tr>
32: <td>
33: <asp:TextBox ID="TextBox4"
34: runat="server"></asp:TextBox>
35: <asp:RequiredFieldValidator
36: ID="RequiredFieldValidator4" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox4">
37: </asp:RequiredFieldValidator>
38: </td>
39: </tr>
40: <tr>
41: <td>
42: <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="javascript:return
43: validatePage();" />
44: <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
45: </td>
46: </tr>
47: </table>
From the above markup you can make out that there are four required field validators and out of the four validators three have validation group property defined. In such a scenario when you click the submit button only the validator which doesn’t have validation group will be executed i.e. RequiredFiedlValidator4 will only be executed. Since there are more than one validation group assigning button’ ValidationGroup wont work as it will execute only validator controls which belong to the assigned validaiton group. Other validator controls belonging to other validation group won’t execute. Also there is no way to specify multiple validaiton group using the ValidationGroup property of the button control.
The way to solve this problem is to call Page_ClientValidate javascript function. Page_ClientValidate is a javascript function generated by ASP.NET. The function takes validation group name as an argument. The javascript function which gets called when the submit button is clicked is pasted below.
1: <script language="javascript" type="text/javascript">
2: function validatePage()
3: {
4: //Executes all the validation controls associated with group1 validaiton Group1.
5: var flag = Page_ClientValidate('Group1');
6: if (flag)
7: //Executes all the validation controls associated with group1 validaiton Group2.
8: flag = Page_ClientValidate('Group2');
9: if (flag)
10: //Executes all the validation controls associated with group1 validaiton Group3.
11: flag = Page_ClientValidate('Group3');
12: if (flag)
13: //Executes all the validation controls which are not associated with any validation group.
14: flag = Page_ClientValidate();
15: return flag;
16: }
17: </script>
In the above code you can see the Page_ClientValidate function is called four times, first by passing the first validation group name, second time passing on the second validation group name and so on. If you see the last call to Page_ClientValidate, it is called with an empty argument. When call Page_ClientValidate with no argume/validation group name it executes all the validation controls in the page which don’t have a validation group assigned to them. So if one wants to execute validation controls which are not having any validation group associated with them you can call the Page_ClientValidate without any parameter.
So using Page_ClientValidate javascript function one can execute all the validation controls in the page by passing the validation group name or without any validation group name.
Try to know more.
Sandeep