SetFocusOnError in ASP.NET 1.x
One of the cool features in ASP.NET 2.0 is the ability to set focus to the input control having invalid input. This is accomplished by setting the SetFocusOnError property of the associated validator control(s) to true. However, the same feature can be achieved in ASP.NET 1.x using the documented and undocumented client-side API of the ASP.NET validation framework. Here is how:
Plug in the following script block in the .aspx file
<
script language="javascript" type="text/javascript">
function setFocus()
{
var refCtl = null;
Page_ClientValidate();
for (var i=0; i<Page_Validators.length; i++)
{
if (!Page_Validators
.isvalid)
{
refCtl = document.getElementById(Page_Validators
.getAttribute("controltovalidate"));
if (refCtl != null)
{
refCtl.focus();
}
break;
}
}
}
</
script>
In the code-behind, have this one liner in the page's load event:
<submit button>.Attributes.Add("onclick", "setFocus();");
This code has been tested for ASP.NET 1.x with Internet Explorer 6+.