Click here to Skip to main content
15,881,173 members
Articles / Web Development / ASP.NET
Article

Transferring Control Focus through Custom Validation File, in ASP.NET

Rate me:
Please Sign up or sign in to vote.
2.73/5 (6 votes)
12 May 2005 35.3K   14   5
Validation controls will not set the focus to the appropriate control when the validation fails. This can be achieved using a custom validation file.

Introduction

In one of my recent projects, I faced the problem of how to transfer control focus to the appropriate control when validation fails. I was confused with the client requirement at first but realized it is a common and basic requirement from the user point. But it is not implemented in the WebUIValidation.js. You can implement this with a minimal setting change in the web.config file and a little code change in the WebUIValidation file.

Add the following element in the system.web section of the web.config file for using a custom validation file:

XML
<webControls clientScriptsLocation="<virtual_path>/javascripts/"></webControls>

Add a new function in the WebUIValidation which will set the focus to the control which fails in the validation process.

JavaScript
function ValidatorControlFocus()
{
    if(typeof(Page_Validators) != "undefined")
    {
        for (i = 0; i < Page_Validators.length; i++)
        {
            val = Page_Validators[i];
            if(val.isvalid == false)
            {
                control = document.all[val.controltovalidate];
                if(control != null)
                {
                    if( !control.isDisabled ){
                        if(control.style.visibility != "hidden"){
                            control.focus();
                        }
                    }
                    return;
                }
            }
        }
    }
}

Call the above function in Page_ClientValidate as well as in ValidatorCommonOnSubmit functions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWorks fine to move the focus, but Pin
totakoori8-Apr-08 13:24
totakoori8-Apr-08 13:24 
GeneralTried your solution but web config file causing error when building in debug mode Pin
Scoochie9-Jul-07 10:55
Scoochie9-Jul-07 10:55 
GeneralRe: Tried your solution but web config file causing error when building in debug mode Pin
bkalicharan10-Jul-07 8:22
bkalicharan10-Jul-07 8:22 
GeneralRe: Tried your solution but web config file causing error when building in debug mode Pin
Scoochie10-Jul-07 11:49
Scoochie10-Jul-07 11:49 
GeneralRe: Tried your solution but web config file causing error when building in debug mode Pin
Scoochie10-Jul-07 12:18
Scoochie10-Jul-07 12:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.