Validation in radwindow form firing multiple times

5 posts, 1 answers
  1. Karel
    Karel avatar
    3 posts
    Member since:
    Aug 2011

    Posted 12 Feb 2015 Link to this post

    In my page I have a radgrid and a radwindowmanager with a couple of radwindows.
    The radwindows have a ContentTemplate with a usercontrol in it.
    In that usercontrol I have a form and some validators and validationsummary.
    The WindowManager and the RadGrid are defined in my ajaxmanager, in both directions.

    The window opens by clicking a commandrowbutton of the grid.

    The issue I'm having is that my validators are firing multiple times.

        - Motivation is empty
        - Motivation is empty
        - Motivation is empty

    Exactly as many times as I have rebound the grid on the page in fact. (times rebound since being on the page or since a successful submit of the window)
    So somehow, the radgrid rebinding causes the validators to be registered again or something like that.

    Should I define my window differently? Open the window differently? Is there a way to reset those validators or that window so it doesn't happen?
  2. Answer
    Marin Bratanov
    Admin
    Marin Bratanov avatar
    3602 posts

    Posted 12 Feb 2015 Link to this post

    Hi,

    The problem stems from moving validators in the DOM and disposing them with AJAX. You can reproduce it with the following code:

    <asp:UpdatePanel ID="Updatepanel1" runat="server">
        <ContentTemplate>
            <div id="test">
                <asp:TextBox ID="Textbox1" runat="server" ValidationGroup="valgrp" />
                <asp:Button ID="Button1" Text="validate" ValidationGroup="valgrp" runat="server" />
                <asp:RequiredFieldValidator ID="Requiredfieldvalidator1" ErrorMessage="errormessage" ControlToValidate="Textbox1" ValidationGroup="valgrp" runat="server" />
            </div>
            <asp:Button ID="Button4" Text="2 - AJAX" runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button ID="Button2" Text="1 - move div" runat="server" OnClientClick="moveDiv(); return false;" />
    <asp:UpdatePanel ID="Updatepanel2" runat="server">
        <ContentTemplate>
            <div id="other"></div>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:ValidationSummary ID="valSummary" runat="server" ValidationGroup="valgrp" ShowMessageBox="true" ShowSummary="false" />
    <script>
        function moveDiv() {
            var testDiv = $get("test");
            $get("other").appendChild(testDiv);
        }
    </script>

    What RadWindow does is that, when shown for the first time, its COntenTemplate is moved from the place of the declaration to a direct child of the <form> because RadWindow renders its UI with JS at this point.

    What I can offer is cleaning up the Page_Validators array in order to avoid the duplicates there:

    function removeValidator(sender, args) {
        while (Page_Validators.length > 0) {
            Page_Validators.pop();
        }
    }
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(removeValidator)

    The caveat with the code above is that it cleans up ALL the validators on the page. Thus, if any are not part of the partial page rendering, they will be lost. So, if you have validators outside of AJAX-enabled content, you will need to tweak this solution. For example, you could use the Sys.Application.Load event in order to loop through the Page_Validators array, find duplicates (e.g., by their IDs) and remove them from the array.

    Another approach you can consider is to simply move the contents to a standalone aspx page and load that in the RadWindow. This will create a separate context for it and it will stop disposing the validators with AJAX in this manner.

    Regards,

    Marin Bratanov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Karel
    Karel avatar
    3 posts
    Member since:
    Aug 2011

    Posted 17 Feb 2015 in reply to Marin Bratanov Link to this post

    Thank you Marin. I figured it would be something like that.
    I fixed it by moving the forms to their own aspx pages.
  5. Karel
    Karel avatar
    3 posts
    Member since:
    Aug 2011

    Posted 17 Feb 2015 in reply to Marin Bratanov Link to this post

    What I still don't completely understand is why a new instance of the window is added to the DOM when the Grid is rebound.
    Is that because they're linked in the AjaxManager?
    It doesn't happen when the window is shown, because I use VisibleOnPageLoad for that, which doesn't make a new window.
  6. Marin Bratanov
    Admin
    Marin Bratanov avatar
    3602 posts

    Posted 17 Feb 2015 Link to this post

    Hi Kerel

    It is not that a new RadWIndow is added, but rather that its content is moved in the DOM. I am attaching two screenshots of the dev toolbar of the browser - before and after showing a RadWindow that illustrate the difference. 

    It is the move in the DOM and afterwards the DOM disposal that the MS AJAX validation scripts do not take into account, and the client-side array of validators grows each time because the old ones do not dispose properly.

    You may also find useful the following articles:

     

    Regards,

    Marin Bratanov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017