1

I have just implemented a RequiredFieldValidatior with ValidationSummary by setting a ValidationGroup.

It works fine, when I click the relevant button!

But the viewport changes and shifts at the top of the page and my validaiton result and the button etc stays at the very bottom of the page which is not nice.

Is there a way to prevent this and let the browser still be shopwing the same area after button click?

Additional Note: The validation fails on the client-side; so no post-back occurs. Basically, validation fails, and the viewport slides at the top of the page.

pencilCake
  • 51,323
  • 85
  • 226
  • 363

3 Answers3

2

You can achieve this by adding the following directive to the top of your page:

<%@ Page MaintainScrollPositionOnPostback="true" %>
Widor
  • 13,003
  • 7
  • 42
  • 64
  • I believe that is something to do after a post-back back. Imagine that I am still on the client-side and the validation fails on the client side. (I have that property set to true as well but I don't think it does not effect my case) – pencilCake Nov 02 '11 at 12:59
  • Ah, I see what you mean about the client-side validation. Combine this with @Tim B James suggestion and it should work? – Widor Nov 02 '11 at 13:02
2

In your RequiredFieldValidator there is an option to SetFocusOnError which will then move the cursor to the Textbox/input type.

Then in your Page declaration at the top of the page, add in MaintainScrollPositionOnPostback="true"

<%@ Page MaintainScrollPositionOnPostback="true" %>

Tim B James
  • 20,084
  • 4
  • 73
  • 103
0

I have done this before to set focus on the fist element that didn't validate when calling validation manually:

//has to be called after Page_ClientValidate()
function ValidatorFocus()
{
    var i;
    for (i = 0; i < Page_Validators.length; i++)
    {
        if (!Page_Validators[i].isvalid)
        {
            document.getElementById(Page_Validators[i].controltovalidate).focus();
            break;
        }
    }
}
rick schott
  • 21,012
  • 5
  • 52
  • 81