<Control>.Focus() from server side doesn't scroll into view

Posted by George on Stack Overflow See other posts from Stack Overflow or by George
Published on 2010-05-01T18:25:22Z Indexed on 2010/05/01 18:27 UTC
Read the original article Hit count: 163

Filed under:
|

Custom Validation Control:

<asp:CustomValidator
ID="valSex" runat="server" ErrorMessage="1.2 &lt;b&gt;Sex&lt;/b&gt; not specified"
EnableClientScript="true" ClientValidationFunction="ValidateSex" SetFocusOnError="True">Selection required</asp:CustomValidator>

Client Side validation routine:

function ValidateSex(source, args) {

    var optMale = document.getElementById("<%=optMale.ClientID %>");
    var optFemale = document.getElementById("<%=optFemale.ClientID %>");

    if (!optMale.checked && !optFemale.checked) {
        args.IsValid = false;
        optMale.focus();
    }
    else
        args.IsValid = true;
}

When the page is submitted and Sex is not specified, focus is set but the 2 radio buttons are not quite in view, vertical scrolling is required to bring them into view.

Shouldn't the Focus() method have brought the focus control into view?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about JavaScript