Custom validator scrolls page to the top in ASP.NET

Custom validator scrolls page to the top in ASP.NET

If we add a ValidationSummary control to the page, its ValidationSummaryOnSubmit function invokes “window.scrollTo(0,0)” when page is invalid. That’s why the page scrolls to the top.

To workaround this issue, we can assign a function to window.scrollTo which do nothing, so that window.scrollTo(0,0) will not take effect. This workaround is appropriate for the pages which doesn’t need window.scrollTo. For example, I have posted the following code for your reference. Hope it is helpful to you.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
         function check(source, args)
         {
            if (args.Value == "a")
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }                          
         }
         window.scrollTo = function(){}
    </script>
</head>

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *