Click here to Skip to main content
Licence 
First Posted 12 May 2005
Views 25,264
Bookmarked 14 times

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

By | 12 May 2005 | Article
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:

<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.

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

About the Author

bkalicharan

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWorks fine to move the focus, but Pinmembertotakoori13:24 8 Apr '08  
GeneralTried your solution but web config file causing error when building in debug mode PinmemberScoochie10:55 9 Jul '07  
GeneralRe: Tried your solution but web config file causing error when building in debug mode Pinmemberbkalicharan8:22 10 Jul '07  
GeneralRe: Tried your solution but web config file causing error when building in debug mode PinmemberScoochie11:49 10 Jul '07  
I have retried and the web config file does not now cause errors on build but I get a message that it cannot find the WebUIValidation file. Here is the path to the file but I am not sure this is what I should be placing in the web config file.

 
That is the actual location of the file but I am not sure I am refferencing it properly. Would appreciate your help.
GeneralRe: Tried your solution but web config file causing error when building in debug mode PinmemberScoochie12:18 10 Jul '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 12 May 2005
Article Copyright 2005 by bkalicharan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid