Sunday, September 27, 2009

Calling multiple validation group

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

19 comments:

  1. Here is the challenge for you. Lets say each Group have validation Summaries. Your solution would display only one Summary at a time, how would you display ALL Group Summaries ?!

    ReplyDelete
  2. If the validation group is on one user control and button is on another user control will the same code will work

    ReplyDelete
  3. Hi Ashwini,
    Yes it will work. You want to call the validation group of another user control from within another user control you can assign the validation group name to the ValidationGroup property of the button. Or else you can write a javascript on the click event of the button to call the required validation groups.

    ReplyDelete
  4. Hi Guru,
    The best design is to have only one validation summary in a page. If you have more than one validation summary then the validation error messages of all the validation groups will be clubbed and shown in all the validation summary. That the reason you don't find multiple validation summary in a page.
    To display all group summaries just call all the validation group and system will show all the error messages in the validation summary. Its as simple as that.

    ReplyDelete
  5. Hi Sandeep,

    Yes ur right now it's working fine but i got one more problem

    i will explain here brefly

    i have one use control inside the usecontrol i have radgrid inside that in the edit template i have controls there i added validation group then coming to main user control i call that group in the button click to validate the group like this

    page.validate("GroupName")

    it's validating but it's not showing the icon near to that filed inside the grid

    do u have any idea please let me know

    thanks in advance.

    ASAP

    ReplyDelete
  6. Hi Sandeep,
    I have one different issue -
    Using Same User Control twice on webpage in different panel having seperate ok button.So how to validate specific user-control on thier respective button click.

    do u have any idea to achieve this.
    thanks

    ReplyDelete
  7. Hi Deepak,
    One way of doing this is to expose a property in the user control called "ValidationGroup" and assigning two different "ValidationGroup" name in the html view of Visual Studio and then assigning these names to the "ValidationGroup" property of the buttons.
    In the set method of the property assign all the input control' "ValidationGroup" property in the user controls to the value provided in the "ValidationGroup" property of the user control. Hope this helps.

    ReplyDelete
  8. Hi it was very usefull. Thanx a lot.

    Avinash Ankur

    ReplyDelete
  9. hi

    I have one issue, i have one aspx page,and on it i have two user control ascx pages.

    i have three checbox. one each in user control and one in aspx page.
    When i click checkbox in 1st usercontrol it fires some events of disabling required feild validators and same haapens in other usercontrol.
    Now i want is, if i click on the checkbox present on aspx page on which i have this user controls,the checkboxes of the user controls should get change accordingly and the events related to them should fire...
    Can any one help me plz...




    Now if i want is if i click

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. Hi Ankur,
    Just put the methods which are called by the user control to disable the validators in the page or external javascript file.
    Now when you click the checkbox in the aspx page retrieve the checkboxes from within the user controls and uncheck them or check them. Once this is done call the respective methods to disable or enable the validators in the user controls. Hope this helps.

    ReplyDelete
  12. Thanks a bunch this helped me alot (Deepaks prob)

    ReplyDelete
  13. Hi Eshanka,
    Happy to know that Deepaks prob helped you solve your prob.

    ReplyDelete
  14. nice article , thank you so much, keep it up

    ReplyDelete
  15. how i assign all validation group to one validation summary

    ReplyDelete
  16. i think we can try like this ...Submit


    base key and updateuser are two groups.

    ReplyDelete
  17. how to validate multiple users & display different labels in a single page wen different users login in to the page using single submit button..

    ReplyDelete
  18. Thank you for the solution hahaha, cheers ! ! ! ! !

    ReplyDelete

Please provide your valuable comments.