Skip to main content
Email Password   helpLost your password?

Sample Image - ClientSideFocus.jpg

Introduction

I've been working on trying to set the focus of client side controls today and thought I'd submit this as I think it could be useful. I hope I haven't missed a method that is already built into ASP.NET that already does this.

I've created a helper class that enables you to set the client side focus with a single call. I decided against creating a subclass of the Page class as I think the helper class approach is better and easier to implement on existing systems.

How it worked originally

All that is required is the ID of the form which the control is in and the ID of the control itself. There are a couple of overloaded functions that automatically find these IDs depending on what type of server control you pass as a parameter.

private void Page_Load(object sender, System.EventArgs e)
{
  if( !this.IsPostBack )
  {
    McGiv.Web.UI.ClientSideFocus.setFocus(this.TextBox1);
    this.ViewState["focus"] = "WebControl";
  }
}

The JavaScript script is then generated and registered to the given Page object.

The script itself makes a copy of the document.body.onload function and replaces it with the focus setting function which in turn calls the original onload function, which means that you won't have to tinker with any existing onload scripts you may have within your pages.

// save original function

 if(document.body.onload)
  original = document.body.onload;

 // override original function

 document.body.onload = document_onLoad;

 function document_onLoad()
 {
  try
  {
   document.FormID.ControlID.focus();
  }
  catch(e){}

  // call original function

  original();
 }


 function original()
 {
  // do nothing if original is null

 }

How it works now

After being notified that the original code could potentially steal the focus of another window (which can be irritating), I switched from using the document.body.onload event to the document.body.onfocus event.

    var called = false;

    if(document.body.onfocus)
        orig_document_body_onfocus = document.body.onfocus;

    document.body.onfocus = document_body_onfocus;


    function setFocus()
    {
        var control = document.getElementById("html");
        if(control)
            control.focus();
    }


    function orig_document_body_onfocus(){}
    
    function document_body_onfocus ()
    {
        if( !called )
        {
            called = true;
            setFocus();
        }

        orig_document_body_onfocus();
    }



    if( window.__smartNav && window.__smartNav.restoreFocus)
    {
        orig_window__smartNav_restoreFocus = window.__smartNav.restoreFocus;
        window.__smartNav.restoreFocus = window__smartNav_restoreFocus;
    }


    function orig_window__smartNav_restoreFocus(){}

    function window__smartNav_restoreFocus()
    {
        setFocus();
        orig_window__smartNav_restoreFocus
    }

I�ve also tried to solve the problem with Smart Navigation by overriding the restoreFocus function from within SmartNav.js file but this seems to only work on my local machine at the moment. This may be due to a security issue or due to the loading time taken from remote hosts.

I removed the need for the ID of the HTML form, as with most cases within ASP.NET, the page will only use one form. This modification alone removes about half the content from the original code. I�ve included the original code if you still need to reference the form's ID as well as the control ID.

Known Issues

Currently, the helper class doesn�t function properly when Smart Navigation is enabled.

The demo

I've included a simple demo that switches focus between a WebControl, HtmlControl and a hard coded HTML input control.

I use the viewstate to keep track of which control has focus.

private void Button1_Click(object sender, System.EventArgs e)
{
  string focus = this.ViewState["focus"] as string;
  if( focus == null )
    focus = "WebControl";
  switch(focus)
  {
    case "Html":
    {
      McGiv.Web.UI.ClientSideFocus.setFocus(this.TextBox1);
      this.ViewState["focus"] = "WebControl";
      break;
    }
    case "HtmlControl":
    {
      McGiv.Web.UI.ClientSideFocus.setFocus(this, "html");
      this.ViewState["focus"] = "Html";
      break;
    }
    case "WebControl":
    {
      McGiv.Web.UI.ClientSideFocus.setFocus(this.HtmlControl1);
      this.ViewState["focus"] = "HtmlControl";
      break;
    }
  }
}

In order to get it to work, unzip the demo files into a virtual directory mapped to http://localhost/ClientSideFocus/.

I've also setup an online demo here.

Enjoy.

Version History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralGodsend Pin
Vodstok
10:32 31 Mar '05  
GeneralSetFocus Pin
kameliar
3:17 23 Mar '05  
GeneralRe: SetFocus Pin
McGiv
3:24 23 Mar '05  
GeneralThanks Pin
iconsam
9:55 10 Mar '05  
GeneralMinor Change... Pin
Jaben C
4:40 12 Oct '04  
GeneralRe: Minor Change... Pin
McGiv
4:46 12 Oct '04  
GeneralRegisterStartupScript Pin
JasonBSteele
5:27 20 Aug '04  
GeneralRe: RegisterStartupScript Pin
McGiv
9:36 20 Aug '04  
GeneralRe: RegisterStartupScript Pin
JasonBSteele
1:22 21 Aug '04  
GeneralBug in Demo. Pin
Cypher
5:37 21 Jan '04  
GeneralRe: Bug in Demo. Pin
McGiv
6:24 21 Jan '04  
GeneralRe: FIXED - Bug in Demo. Pin
McGiv
6:48 21 Jan '04  
GeneralUpdated Code Pin
McGiv
15:25 20 Jan '04  
GeneralFocus is evil Pin
Stephane Rodriguez.
7:15 13 Jan '04  
GeneralRe: Focus is evil Pin
McGiv
7:47 13 Jan '04  
GeneralRe: Focus is evil Pin
Anonymous
1:36 20 Jan '04  
GeneralRe: Focus is evil Pin
Stephane Rodriguez.
20:14 20 Jan '04  
GeneralRe: Focus is evil Pin
Jörgen Sigvardsson
9:03 13 Jan '04  
GeneralRe: Focus is evil Pin
Peter Hendrix
22:25 19 Jan '04  
GeneralRe: Focus is evil Pin
Stephane Rodriguez.
22:43 19 Jan '04  
GeneralRe: Focus is evil Pin
Anonymous
11:45 20 Jan '04  
GeneralRe: Focus is evil Pin
Anonymous
16:44 29 Aug '05  
GeneralRe: Focus is evil Pin
McGiv
15:28 20 Jan '04  


Last Updated 19 Jan 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009