views:

2410

answers:

4

I have a simple form with a few required field validators and a validation summary control. When I submit the form, the client validation will cause the form to jump to the top of the page. If i remove the validation summary, the page does not move.

Heres a quick example (pardon the line breaks):

<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<asp:TextBox ID="test" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="testrequired" runat="server" ControlToValidate="test">*</asp:RequiredFieldValidator>
<asp:ValidationSummary ID="summary" runat="server" />
<asp:Button ID="submit" runat="server" Text="submit" />

I've tried setting SetFocusOnError="true" in the required field validator and MaintainScrollPositionOnPostback="true" for giggles - even though this isn't a postback - without any luck. Is there a known solution for this problem?

EDIT:

I've found the problem in the js generated by WebResource.axd. Seems to come down to a single line in the ValidationSummaryOnSubmit() function.

line 534: window.scrollTo(0,0);

Any ideas on how to remove or bypass this?

EDIT2:

Quick work around for the time being:

  • set EnableClientScript="false" for all validation controls (disabling client validation)
  • set MaintainScrollPositionOnPostback="true" in Page directive

Still hoping for a client side solution...

EDIT3:

It seems a better work around is to just override the window.scrollTo() function so that it does nothing when called by the validation script:

<script type="text/javascript">
    window.scrollTo = function() { }
</script>

Adding the above anywhere on the page leaves the client validation in tact, but disables the window.scrollTo() method throughout the page

A: 

Look at setting the target schema for your html.

Try http://msdn.microsoft.com/en-us/library/6379d90d(VS.71).aspx

Prior to VS 2005 you could set the schema on a page by page level.

Just a thought.

Dan Vallejo
I'm not sure I follow. This doesn't appear to be browser specific
Adam
+1  A: 

The page is going to jump to wherever your validation summary is. If you'd like it to stay near the bottom, move the validation summary down near the submit button.

EDIT, you can also try turning the validation summary off.

SauceMaster
@sausemaster, the page jumps to the top no matter where I put the validation summary. when I turn the validation summary off, it does not jump around, but unfortunately thats the one thing thats required
Adam
Ok. I think you may be stuck writing some javascript to have a control call focus when submit is clicked of something similar. A quick google search returned this forum (http://forums.asp.net/t/1089469.aspx) topic. Sorry and best of luck.
SauceMaster
I have 3 summaries, and it always jumps to the top.
cosmo0
A: 

Perhaps you could inherit from the required fieldvalidator and override the client side validation in a custom control?

scottschulthess
+10  A: 

Two possible work arounds:

Disable client validation and jump to correct position on post back:

* set EnableClientScript="false" for all validation controls (disabling client validation)
* set MaintainScrollPositionOnPostback="true" in Page directive

Disable the scrollTo function in javascript:

<script type="text/javascript">
    window.scrollTo = function() { }
</script>
Adam
This is what fixed it for me:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MaintainScrollPositionOnPostback="true" %>
Adam Berent
This won't focus the summary at top if there are two summaries on page (1 at top, 2nd at bottom)
Popo