I have a simple test page with a TextBox, a RequiredFieldValidator, a Submit Button and a ValidationSummary. There is a lot of other stuff (only simple text for instance) so that the submit button which is at the bottom of the page is not visible. One has
to scroll down to reach the button. The ValidationSummary is below the Button:
If I leave the TextBox empty and click on Submit the validation fails as expected and the RequiredFieldValidator the ValidationSummary display the ErrorMessage.
Note: No Postback to the server occurs, the validation happens on client side! (which is OK)
But disturbing is that the page moves ("jumps") to top position. So the ValidationSummary is not directly visible to the user. He has to scroll down again to the Button to see the text in the ValidationSummary.
Is there a way to prevent this page jump to top position? Basically I don't want the page to move to make sure that the user can see the validation summary text at once when he clicks on submit and validation fails.
It works, but I'm not quite happy with that postback I have now for validation.
Is there no way to maintain the scroll position purely on client side (with enabled client script)?
What is confusing me is the fact that this "page jump" seems to depend on the ValidationSummary on the page. If I remove it and let only the RequiredFieldValidator write the ErrorMessage beside the TextBox the page does not move after failed validation.
Is this behaviour of the ValidationSummary intended or is it a bug?
Slauma
Member
40 Points
29 Posts
How To prevent a page with ValidationSummary to jump to top position after failed validation?
Apr 11, 2010 02:06 PM|LINK
I have a simple test page with a TextBox, a RequiredFieldValidator, a Submit Button and a ValidationSummary. There is a lot of other stuff (only simple text for instance) so that the submit button which is at the bottom of the page is not visible. One has to scroll down to reach the button. The ValidationSummary is below the Button:
<asp:TextBox ID="TextBox1" runat="server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" ControlToValidate="TextBox1" ErrorMessage="Input required." Display="None" /> ... lots of text ... <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" /> <br /><br /> <asp:ValidationSummary ID="ValidationSummary1" runat="server" />If I leave the TextBox empty and click on Submit the validation fails as expected and the RequiredFieldValidator the ValidationSummary display the ErrorMessage.
Note: No Postback to the server occurs, the validation happens on client side! (which is OK)
But disturbing is that the page moves ("jumps") to top position. So the ValidationSummary is not directly visible to the user. He has to scroll down again to the Button to see the text in the ValidationSummary.
Is there a way to prevent this page jump to top position? Basically I don't want the page to move to make sure that the user can see the validation summary text at once when he clicks on submit and validation fails.
Thanks for help in advance!
Yousef_Jadal...
Star
8822 Points
1502 Posts
Re: How To prevent a page with ValidationSummary to jump to top position after failed validation?
Apr 11, 2010 03:01 PM|LINK
In the @page tag add MaintainScrollPositionOnPostback="true" .
Then in your Required Validation Control set EnableClientScript to false.
<asp:RequiredFieldValidator ID="RequiredFieldValidator" EnableClientScript="false" runat="server" ControlToValidate="TextBox1" ErrorMessage="Input required." Display="None" />
Slauma
Member
40 Points
29 Posts
Re: How To prevent a page with ValidationSummary to jump to top position after failed validation?
Apr 11, 2010 03:18 PM|LINK
Thanks Yousef for reply!
It works, but I'm not quite happy with that postback I have now for validation.
Is there no way to maintain the scroll position purely on client side (with enabled client script)?
What is confusing me is the fact that this "page jump" seems to depend on the ValidationSummary on the page. If I remove it and let only the RequiredFieldValidator write the ErrorMessage beside the TextBox the page does not move after failed validation.
Is this behaviour of the ValidationSummary intended or is it a bug?
ramiramilu
All-Star
95503 Points
14106 Posts
Re: How To prevent a page with ValidationSummary to jump to top position after failed validation?
Apr 11, 2010 03:55 PM|LINK
Hello,
Just add this...
<script type="text/javascript">
window.scrollTo = function( x,y ) {
return true;
}
</script>
thats it...no need to call the above javascript from anywhere....
It will maintain the scroll...
JumpStart
Slauma
Member
40 Points
29 Posts
Re: How To prevent a page with ValidationSummary to jump to top position after failed validation?
Apr 11, 2010 08:58 PM|LINK
Thank you, this works fine !
jjmonty
Member
102 Points
207 Posts
Re: How To prevent a page with ValidationSummary to jump to top position after failed validation?
Oct 31, 2012 07:13 PM|LINK
Hi, thanks for finding a solution to this major Microsoft f***kup....
I have another issue regarding this problem. I got your solution to work, but the problem is that I have two validation summaries on the same page.
One (I want to the scroll to the top), the other ( I do not).
Any suggestions???