<Control>.Focus() from server side doesn't scroll into view
- by George
Custom Validation Control:
<asp:CustomValidator
ID="valSex" runat="server" ErrorMessage="1.2 <b>Sex</b> 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?