views:

2593

answers:

7

I've got a particularly large form in an page. When the form is validated and a field is invalid, I want to scroll the window to that control. Calling the control's Focus() doesn't seem to do this. I've found a JavaScript workaround to scroll the window to the control, but is there anything built into ASP.NET?

+1  A: 

You should looks into jQuery and the ScrollTo plugin

http://demos.flesler.com/jquery/scrollTo/

Superdumbell
A: 

Adding MaintainScrollPositionOnPostback is the closest that ASP.NET has built in, but won't necessarily jump to the invalid field(s).

<%@ Page MaintainScrollPositionOnPostback="true" %>
Lance Harper
That is not what he's describing - he wants the page to jump to the part of the form which failed validation, not necessarily the position the user was at last time.
Rex M
It doesn't answer the question, but he did solve *my* problem. Thanks!
Randy Stegbauer
+1  A: 

Are you sure Focus() won't do what you're describing? Under the hood, it is essentially doing the "JavaScript workaround" - it writes some JS to the page which calls focus() on the control with the matching ID:

Whichever control had Focus() called last before the page finishes processing writes this to the page:

<script type="text/javascript">
//<![CDATA[
WebForm_AutoFocus('txtFocus2');//]]>
</script>
Rex M
A: 

I've achieved something similar using basic HTML fragments. You just leave an element with a known ID:

<span id="CONTROL-ID"></span>

And then either via script, on on the server side change the url:

window.location += "#CONTROL-ID";

In the first case the page won't reload, it will just scroll down to the control.

DreamSonic
A: 

SO I believe the problem is because I was trying to focus on HtmlGenericControls instead of WebControls.

I just ended up doing a workaround based off of:

http://ryanfarley.com/blog/archive/2004/12/21/1325.aspx http://www.codeproject.com/KB/aspnet/ViewControl.aspx

...in the interest of time.

public static void ScrollTo(this HtmlGenericControl control)
{
    control.Page.RegisterClientScriptBlock("ScrollTo", string.Format(@"

     <script type='text/javascript'> 

      $(document).ready(function() {{
       var element = document.getElementById('{0}');
       element.scrollIntoView();
       element.focus();
      }});

     </script>

    ", control.ClientID));
}

Usage:

if (!this.PropertyForm.Validate())
{
    this.PropertyForm.ErrorMessage.ScrollTo();
    failed = true;
}

(Although it appears Page.RegisterClientScriptBlock() is deprecated for Page.ClientScript.RegisterClientScriptBlock()).

Chris
+1  A: 

Are you using a Validation Summary on your page?

If so, ASP.NET renders some javascript to automatically scroll to the top of the page which may well override the automatic behaviour of the client side validation to focus the last invalid control.

Also, have you turned client side validation off?

If you take a look at the javascript generated by the client side validation you should see methods like this:

function ValidatorValidate(val, validationGroup, event) {
  val.isvalid = true;
  if ((typeof(val.enabled) == "undefined" || val.enabled != false) && 
      IsValidationGroupMatch(val, validationGroup)) {
    if (typeof(val.evaluationfunction) == "function") {
      val.isvalid = val.evaluationfunction(val);
      if (!val.isvalid && Page_InvalidControlToBeFocused == null &&
          typeof(val.focusOnError) == "string" && val.focusOnError == "t") {
        ValidatorSetFocus(val, event);
      }
    }
  }
  ValidatorUpdateDisplay(val);
}

Note the call to ValidatorSetFocus, which is a rather long method that attempts to set the focus to the control in question, or if you have multiple errors, to the last control that was validated, using (eventually) the following lines:

if (typeof(ctrl.focus) != "undefined" && ctrl.focus != null) {
  ctrl.focus();
  Page_InvalidControlToBeFocused = ctrl;
}

To get this behaviour to work, you would ideally need to ensure that all your validators are set to be client-side - server side validators will obviously require a postback, and that might affect things (i.e. lose focus/position) - and setting MaintainScrollPositionOnPostBack to true would probably cause the page to reload to the submit button, rather than the invalid form element.

Using the server side .Focus method will cause ASP.NET to render out some javascript "on the page load" (i.e. near the bottom of the page) but this could be being overriden by one of the other mechanisms dicussed above.

Zhaph - Ben Duguid
A: 

Very simple solution is to set the SetFocusOnError property of the RequiredFieldValidator (or whichever validator control you are using) to true

Stephen lacy